기계 학습에서의 랜덤 포레스트(영어:random forest)는 분류, 회귀 분석 등에 사용되는 앙상블 학습 방법의 일종으로, 훈련 과정에서 구성한 다수의 결정 트리로부터 부류(분류) 또는 평균 예측치(회귀 분석)를 출력함으로써 동작한다.
정의
랜덤 포레스트는 여러 개의 결정 트리들을 임의적으로 학습하는 방식의 앙상블 방법이다. 랜덤 포레스트 방법은 크게 다수의 결정 트리를 구성하는 학습 단계와 입력 벡터가 들어왔을 때, 분류하거나 예측하는 테스트 단계로 구성되어있다. 랜덤 포레스트는 검출, 분류, 그리고 회귀 등 다양한 애플리케이션으로 활용되고 있다.
역사
랜덤 포레스트의 초기 발전은 단일 트리를 확장하는 맥락에서 이용 가능한 결정(available decisions)에 대한 임의의 부분집합(random subset)에 대해 검색하는 아이디어를 도입한 얄리 아미트(Yali Amit)와 도널드 게먼(Donald Geman)의 연구[1]에 영향을 받았다. 또한 임의의 부분공간(random subspace)을 선택하는 틴 캄 호(Tin Kam Ho)의 아이디어[2] 역시 랜덤 포레스트의 디자인에 영향을 미쳤다. 포레스트가 성장할 때, 각 트리를 맞추기(fitting) 전에 임의로 선택한 부분공간으로 훈련 데이터를 투영(projection) 시키는 과정에서 트리 사이에 변형이 일어난다.
현재의 랜덤 포레스트의 개념은 레오 브레이먼(Leo Breiman)의 논문[3]에서 만들어졌다. 이 논문에서는 임의 노드 최적화(randomized node optimization, RNO)와 배깅(bootstrap aggregating, bagging)을 결합한 방법과 같은 CART(classification and regression tree)를 사용해 상관관계가 없는 트리들로 포레스트를 구성하는 방법을 제시하였다.
동기
일반적으로 결정 트리를 이용한 방법의 경우, 그 결과 또는 성능의 변동 폭이 크다는 결점을 가지고 있다. 특히 학습 데이터에 따라 생성되는 결정 트리가 매우 달라지기 때문에 일반화하여 사용하기에 매우 어려움이 따른다. 특히, 결정 트리는 계층적 접근방식이기 때문에 만약 중간에 에러가 발생한다면 다음 단계로 에러가 계속 전파되는 특성을 가진다. 배깅(Bagging) 또는 임의 노드 최적화(Randomized node optimization)와 같은 임의화 기술은 결정 트리가 가진 이러한 단점을 극복하고 좋은 일반화 성능을 갖도록 한다.
ChatScript is the next generation chatbot engine that has won the Loebner's 4 times and is the basis for natural language company for a variety of tech startups.
ChatScript is a rule-based engine, where rules are created by humans writers in program scripts through a process called dialog flow scripting. These use a scripting metalanguage (simply called a "script") as their source code. Here what a ChatScript script file looks like:
#
# file: food.top
#
topic: ~food []
#! I like spinach
s: ( I like spinach ) Are you a fan of the Popeye cartoons?
a: ( ~yes ) I used to watch him as a child. Did you lust after Olive Oyl?
b: ( ~no ) Me neither. She was too skinny.
b: ( yes ) You probably like skinny models.
a: ( ~no ) What cartoons do you watch?
b: ( none ) You lead a deprived life.
b: ( Mickey Mouse ) The Disney icon.
#! I often eat chicken
u: ( ![ not never rarely ] I * ~ingest * ~meat ) You eat meat.
#! I really love chicken
u: ( !~negativeWords I * ~like * ~meat ) You like meat.
#! do you eat bacon?
?: ( do you eat _ [ ham eggs bacon] ) I eat '_0
#! do you like eggs or sushi?
?: ( do you like _* or _* ) I don't like '_0 so I guess that means I prefer '_1.
#! I adore kiwi.
s: ( ~like ~fruit ![~animal _bear] ) Vegan, you too...
#! do you eat steak?
?: ( do you eat _~meat ) No, I hate _0.
#! I eat fish.
s: ( I eat _*1 > )
$food = '_0
I eat oysters.
Take this project and put it into some directory on your machine (typically we call the directory ChatScript, but you can name it whatever). That takes care of installation.
Standalone mode - run locally on a console (for developement/test)
From your ChatScript home directory, go to the BINARIES directory:
cd BINARIES
And run the ChatScript engine
Windows
ChatScript
Linux
./LinuxChatScript64 local
Note: to set the file executable: chmod a+x ./LinuxChatScript64
MacOS
./MacChatScript local
This will cause ChatScript to load and ask you for a username. Enter whatever you want. You are then talking to the default demo bot Harry.
Server Mode (for production)
From your ChatScript home directory, go to the BINARIES directory and run the ChatScript engine as server
Run the server on Windows
ChatScript port=1024
Run the server on Linux
./LinuxChatScript64
Run the server on MacOS
./MacChatScript
This will cause ChatScript to load as a server. But you also need a client (to test client-server communication). You can run a separate command window and go to the BINARIES directory and type
Run a client (test) on Windows
ChatScript client=localhost:1024
Run a client (test) on Linux
./LinuxChatScript64 client=localhost:1024
Run a client (test) on MacOS
./MacChatScript client=localhost:1024
This will cause ChatScript to load as a client and you can talk to the server.
How to build a bot
Run ChatScript locally. From the ChatScript command prompt, type
:build Harry
or whatever other preinstalled bot exists. If you have revised basic data, you can first:
:build 0
How to compile the engine.
On windows if you have Visual Studio installed, launch VS2010/chatscript.sln or VS2015/chatscript.sln and do a build. The result will go in the BINARIES directory.
On Linux, go stand in the SRC directory and type make server (assuming you have make and g++ installed). This creates BINARIES/ChatScript, which can run as a server or locally. There are other make choices for installing PostGres or Mongo.
처음에 ID와 비밀번호를 users라는 사전에 담았습니다. 그리고 users.txt라는 파일을 새로 열어서 f라고 했구요. 그 다음에는 오이지(pickle^^;) 모듈의 덤프(dump)를 사용했습니다.
여러분 덤프 트럭 다 아시죠? 왜 공사장에서 흙 싣고 다니는 무지 큰 트럭 있잖아요. 그런 차는 뒤쪽 짐칸을 들어올려서 흙을 와르르 쏟아내지 않습니까? 여기서 보시는 dump도 마찬가지입니다. users라는 리스트의 내용을 파일 f에 와르르 쏟아붓는 거지요. 음, 이렇게까지 설명할 필요는 없을 텐데… 오늘 쓸 것이 없다보니…
이제 메모장으로 users.txt 파일을 열어보시면 모양이 좀 지저분하긴 해도 데이터가 다 들어있는 걸 보실 수 있을 거예요.
그렇다면 이 파일에 들어있는 것을 원래대로 돌려볼까요? 원래대로 돌리는 것도 역시 오이지 모듈이 할 일이겠죠.
>>> f = open('d:/python21/exam/users.txt')
>>> a = pickle.load(f)
>>> print a
{'sun80': '393948', 'kim': '3kid9', 'ljm': 'py90390'}
사실 방금 보여드린 것은 그리 복잡할 것도 없지만 pickle 모듈은 파이썬에서 만들어지는 것은 뭐든지 다 파일에 적을 수 있다고 합니다. '그게 뭐 어때서'라고 생각하실지 모르겠지만 이건 놀라운 기능이라고 튜토리얼에 쓰여있습니다.
오이지는 이쯤 해두고, 전에 잠깐 구경했던 glob 모듈에 대해 알아보도록 하죠. glob는 파일들의 목록을 뽑을 때 사용하는데, 파일의 경로명을 이용해서 입맛대로 요리할 수 있답니다.