{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# **나이브 베이즈 모델을 이용한 스팸메일 분류기**\n", "Calssification" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **1 분류기 Classification**\n", "1. **Binary Classification** (이진 분류기) : **True / False 조건을** 구분한다\n", "1. **Multiclass Classification** (다변량 분류) : **다양한 클래스간의 조건을** 구분한다\n", "1. **Multi-label Classification** (다중 클래스 레이블 분류) : 다중의 클래스간 **겹치는 조건에서** 구분을 한다\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **2 텍스트 분류기 Classification**\n", "1. **긍정/ 부정, 긍정/ 중립/ 부정** 분류기\n", "1. **뉴스의 토픽** 분류기 (**class 간 중첩되어** 분류가 가능하다)\n", "1. **Named Entity Recognition** (개체명 분류기) : ex) Naive Bayse, Support Vector Machine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **3 Naive Bayse Classification 개념**\n", "1. 확률 기반의 분류기\n", "1. **Naive :** 예측을 위한 Token 들이 **Mutually Independent** (상호독립적)을 가정\n", "1. **Bayse :** 관찰한 Token이 **클래스 전체 대비, 특정 클래스 속할 확률을 Bayse 기반** 으로 계산\n", "\n", "> **Naive Bayse 메커니즘**\n", "\n", "1. 스팸메일과, 정상메일로 구분된 데이터를 사용한다 [download](http://www.aueb.gr/users/ion/data/enron-spam/preprocessed/enron1.tar.gz)\n", "1. 단어 **Token을** 대상으로 **스팸여부를** 학습한다.\n", "1. Data 추가시 잘못 예측한 결과에 대해 **Laplace Smoothing** 으로 보완한 값을 **Bayse 로 공식을** 수정한다" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## **4 Naive Bayse 구현하기**\n", "스펨메일 데이터 다운받기 [download](http://www.aueb.gr/users/ion/data/enron-spam/preprocessed/enron1.tar.gz)\n", "
\n", "### **01 enron 메일데이터 살펴보기**\n", "1. **Summary.txt** 파일에 저장된 내용 살펴보기\n", "1. **정상메일 (3,672개)** 와 **스펨메일 (1,500)개로** 약 1:2의 비율로 구분이 된다" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Legitimate\n", "----------\n", "- Owner: farmer-d\n", "- Total number: 3672 emails\n", "- Date of first email: 1999-12-10\n", "- Date of last email: 2002-01-11\n", "- Similars deletion: No\n", "- Encoding: No\n", "\n", "\n", "Spam\n", "----\n", "- Owner: GP\n", "- Total number: 1500 emails\n", "- Date of first email: 2003-12-18\n", "- Date of last email: 2005-09-06\n", "- Similars deletion: No\n", "- Encoding: No\n", "\n", "Spam:Legitimate rate = 1:3\n", "Total number of emails (legitimate + spam): 5975\n", "\n" ] } ], "source": [ "# 스팸메일 데이터 Summary\n", "with open('./data/enron1/Summary.txt', 'r') as f:\n", " summary = f.read()\n", "print(summary)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Subject: mcmullen gas for 11 / 99\n", "jackie ,\n", "since the inlet to 3 river plant is shut in on 10 / 19 / 99 ( the last day of\n", "flow ) :\n", "at what meter is the mcmullen gas being diverted to ?\n", "at what meter is hpl buying the residue gas ? ( this is the gas from teco ,\n", "vastar , vintage , tejones , and swift )\n", "i still see active deals at meter 3405 in path manager for teco , vastar ,\n", "vintage , tejones , and swift\n", "i also see gas scheduled in pops at meter 3404 and 3405 .\n", "please advice . we need to resolve this as soon as possible so settlement\n", "can send out payments .\n", "thanks\n" ] } ], "source": [ "# ham 폴더에 저장된 메일내용 확인 (정상으로 분류된 메일)\n", "file_path = './data/enron1/ham/0007.1999-12-14.farmer.ham.txt'\n", "with open(file_path, 'r') as infile:\n", " ham_sample = infile.read()\n", "print(ham_sample)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Subject: stacey automated system generating 8 k per week parallelogram\n", "people are\n", "getting rich using this system ! now it ' s your\n", "turn !\n", "we ' ve\n", "cracked the code and will show you . . . .\n", "this is the\n", "only system that does everything for you , so you can make\n", "money\n", ". . . . . . . .\n", "because your\n", "success is . . . completely automated !\n", "let me show\n", "you how !\n", "click\n", "here\n", "to opt out click here % random _ text\n", "\n" ] } ], "source": [ "# spam 폴더에 저장된 메일내용 확인 (스팸으로 분류된 메일)\n", "file_path = './data/enron1/spam/0058.2003-12-21.GP.spam.txt'\n", "with open(file_path, 'r') as infile:\n", " spam_sample = infile.read()\n", "print(spam_sample)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **02 enron 메일 데이터 분류하기**\n", "1. 스펨메일과 정상메일을 레이블을 사용하여 분류한다\n", "1. 1 : 스펨메일, 0 : 정상메일\n", "1. 분류된 데이터를 전처리 과정을 진행한다" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import glob,os\n", "# 정상매일은 0, 스펨매일은 1\n", "emails, labels = [], []\n", "for no, file_path in enumerate(['./data/enron1/ham/','./data/enron1/spam/']):\n", " for filename in glob.glob(os.path.join(file_path, '*.txt')):\n", " with open(filename, 'r', encoding = \"ISO-8859-1\") as infile:\n", " emails.append(infile.read())\n", " labels.append(no)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **03 enron 메일 데이터 임베딩**\n", "1. Chapter 2 에서 진행한 내용을 바탕으로 전처리 작업을 진행한다\n", "1. **숫자와 구두점** 제거, **StopWords** 제거, **표제어 원형** 복원\n", "1. 정제된 데이터로 **희소벡터 (Sparse Vector)** 로 임베딩 ex) (**row index, feacture/term index**)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'fw customer list this one includes more financial counterparties non eol original message from winfree o neal d sent wednesday december am to martin thomas a subject customer list tom attached are eol customer between july and nov broken out by physical and financial the only physical customer i remember non eol are imperial sugar and texas energy i m also checking this list against other non eol deal eric or joe might have done but for now take a look at this o'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nltk.corpus import names\n", "from nltk.stem import WordNetLemmatizer\n", "all_names = set(names.words())\n", "lemmatizer = WordNetLemmatizer()\n", "\n", "# 표제어 복원작업\n", "def clean_text(docs):\n", " cleaned = [' '.join([lemmatizer.lemmatize(word.lower())\n", " for word in doc.split()\n", " if word.isalpha() and word not in all_names]) \n", " for doc in docs]\n", " return cleaned\n", "\n", "# 사용자 함수를 활용하여 전처리 작업을 진행한다\n", "cleaned_emails = clean_text(emails)\n", "cleaned_emails[0]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "모델의 Type: \n", "임베딩의 크기: (5172, 500)\n", "0번문장 내용보기: \n", " (0, 248)\t1\n", " (0, 102)\t1\n", " (0, 125)\t1\n", " (0, 435)\t1\n", " (0, 224)\t1\n", " (0, 30)\t1\n", " (0, 447)\t1\n", " (0, 417)\t1\n", " (0, 104)\t1\n", " (0, 482)\t1\n", " (0, 390)\t1\n", " (0, 265)\t1\n", " (0, 307)\t1\n", " (0, 147)\t2\n", " (0, 241)\t3\n", " (0, 94)\t4\n", " (0, 162)\t1\n", "CPU times: user 611 ms, sys: 7.26 ms, total: 618 ms\n", "Wall time: 617 ms\n" ] } ], "source": [ "%%time\n", "# 출현빈도가 높은 상위 500개의 Token을 대상으로 임베딩 한다\n", "# 희소벡터(Sparse Vector)로 변환 : (row index, feacture/term index)\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "cv = CountVectorizer(stop_words=\"english\", max_features=500)\n", "term_docs = cv.fit_transform(cleaned_emails)\n", "print(\"모델의 Type: {}\\n임베딩의 크기: {}\\n0번문장 내용보기: \\n{}\".format(\n", " type(term_docs),\n", " term_docs.shape, # 5,172개 문장을 500개 단어로 생성\n", " term_docs [0])) # 0번 문장의 단어 Vector 목록을 출력" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['able', 'access', 'account', 'accounting', 'act', 'action', 'activity']\n", "0 : able\n", "162 : fw\n", "481 : website\n", "357 : read\n", "125 : energy\n" ] } ], "source": [ "# cv 모델로 인덱스별 단어 Token 내용보기\n", "# feature_mapping = cv.vocabulary_ # dict 로 내용출력 (key:value)\n", "\n", "print(cv.get_feature_names()[:7])\n", "feature_names = cv.get_feature_names() # List 로 내용출력 (인덱스별 value)\n", "for indx in [0, 162, 481, 357, 125]:\n", " print(indx, \":\", feature_names[indx])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **04-1 Naive Bayse 학습을 위한 준비작업**\n", "모델의 학습을 위한 준비작업으로 데이터를 그룹화 한다" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_keys([0, 1])\n" ] }, { "data": { "text/plain": [ "[3672, 3673, 3674, 3675, 3676, 3677, 3678, 3679, 3680, 3681]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 레이블을 기준으로 데이터를 그룹화 한다\n", "# defaultdict : 스팸여부 0,1 Tag 로 Token Index List 생성\n", "def get_label_index(labels):\n", " from collections import defaultdict\n", " label_index = defaultdict(list)\n", " for index, label in enumerate(labels):\n", " label_index[label].append(index)\n", " return label_index\n", "\n", "# 0 ~ 3600 : 정상메일[0], 3600 ~ 나머지 : 스팸메일[1]\n", "label_index = get_label_index(labels)\n", "print(label_index.keys())\n", "label_index[1][:10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **04-2 Naive Bayse 위한 사전확률/ 우도값 계산**\n", "**사전확률 및 우도값을** 계산하는 함수를 정의한다" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 0.7099767981438515, 1: 0.2900232018561485}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 학습 샘플을 활용하여 사전 확률을 계산 \n", "def get_prior(label_index):\n", " \"\"\" Compute prior based on training samples\n", " Args: label_index (grouped sample indices by class)\n", " Returns: { 단어 key : corresponding prior } \"\"\"\n", " prior = {label: len(index) for label, index in label_index.items()}\n", " total_count = sum(prior.values())\n", " for label in prior:\n", " prior[label] /= float(total_count)\n", " return prior\n", "\n", "# 위의 인덱스 데이터를 활용하여 사전확률을 계산한다\n", "prior = get_prior(label_index)\n", "prior" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "우도값 shape : (500,)\n", "단어 내용보기 : ['able', 'access', 'account', 'accounting', 'act']\n", "우도값 array :\n", "[1.08580656e-03 9.57737068e-04 8.79781725e-04 8.46372292e-04\n", " 1.00228298e-04 2.39434267e-04 1.97115652e-03 1.34194554e-03\n", " 1.84308703e-03 8.35235815e-04 1.08023832e-03 1.03012417e-03\n", " 1.67047163e-05 1.50342447e-04 7.62848711e-04 8.40804054e-04\n", " 8.12962860e-04 2.22172727e-03 1.99342948e-03 5.01141489e-05]\n" ] } ], "source": [ "# 확률적 유사가능도(최대 가능도 추정)를 계산: 빈도상위 500개의 단어로 조건부 확률 p(feature|spam)을 계산\n", "import numpy as np\n", "def get_likelihood(term_document_matrix, label_index, smoothing=0):\n", " \"\"\" 훈련 데이터로 우도값 측정\n", " Args: term_document_matrix, label_index, smoothing \n", " Returns: { 단어 key, 동시확률 P(feature|class) }\n", " \"\"\"\n", " likelihood = {}\n", " for label, index in label_index.items():\n", " likelihood[label] = term_document_matrix[index, :].sum(axis=0) + smoothing\n", " likelihood[label] = np.asarray(likelihood[label])[0]\n", " total_count = likelihood[label].sum()\n", " likelihood[label] = likelihood[label] / float(total_count)\n", " return likelihood\n", "\n", "smoothing = 1 # 라플라스 스무딩\n", "likelihood = get_likelihood(term_docs, label_index, smoothing)\n", "print(\"우도값 shape : {}\\n단어 내용보기 : {}\\n우도값 array :\\n{}\".format(\n", " likelihood[0].shape, # 0번 레이블일 때 단어별 우도값 계산\n", " feature_names[:5], # 인덱스별 단어 확인\n", " likelihood[0][:20])) # 0번 레이블의 단어별 우도값 샘플 [:20]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **04-3 자연 Log 를 활용한 예측함수 구현하기**\n", "- 앞에서 측정한 **사전확률과 및 우도값을** 활용하여 예측함수를 정의 합니다\n", "- 단어들의 확률을 합치기 위해, **Log()** 로 변환 후 **경우의 수를 모두 합칩니다**" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# OverFlow가 발생 가능하므로, 데이터를 Log() 자연로그로 변환 후 덧셈 계산,\n", "# 계산이 끝난 뒤, 로그의 역함수 (exp()) 를 활용하여 실수로 변환한다\n", "def get_posterior(term_document_matrix, prior, likelihood):\n", " \"\"\" 사전확률과 유사가능도를 바탕으로 샘플 데이터의 사후확률을 계산\n", " Args:\n", " term_document_matrix (sparse matrix)\n", " prior { 단어 Key : 사전확률 }\n", " likelihood { 단어 Key : 조건부 확률 }\n", " Returns: { 단어 Key : 관련 사후 확률값 }\n", " \"\"\"\n", " # 확률의 연산시 log() 로 변환한 후 합친다\n", " num_docs, posteriors = term_document_matrix.shape[0], []\n", " for i in range(num_docs):\n", " # 사후확률 : 사전확률 X 유사가능도(최대 가능도 추정량)\n", " posterior = {key: np.log(prior_label) for key, prior_label in prior.items()}\n", " for label, likelihood_label in likelihood.items():\n", " term_document_vector = term_document_matrix.getrow(i)\n", " counts = term_document_vector.data\n", " indices = term_document_vector.indices\n", " for count, index in zip(counts, indices):\n", " posterior[label] += np.log(likelihood_label[index]) * count\n", " # exp(-1000):exp(-999) 는 분모가 0이 되는 문제가 발생\n", " # 하지만 exp(0):exp(1)과 동치가 된다.\n", " min_log_posterior = min(posterior.values())\n", " for label in posterior:\n", " try: posterior[label] = np.exp(posterior[label] - min_log_posterior)\n", " except: posterior[label] = float('inf') # 값이 너무 클때\n", " # 전체 합이 1이 되도록 정규화\n", " sum_posterior = sum(posterior.values())\n", " for label in posterior:\n", " if posterior[label] == float('inf'): posterior[label] = 1.0\n", " else: posterior[label] /= sum_posterior\n", " posteriors.append(posterior.copy())\n", " return posteriors" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{0: 0.9815828838777807, 1: 0.018417116122219333},\n", " {0: 1.5274461154428757e-06, 1: 0.9999984725538845}]\n" ] } ], "source": [ "# 테스트 메일을 사용하여 알고리즘을 검증\n", "emails_test = [\n", " '''Subject: flat screens hello ,\n", " please call or contact regarding the other flat screens requested .\n", " trisha tlapek - eb 3132 b michael sergeev - eb 3132 a\n", " also the sun blocker that was taken away from eb 3131 a .\n", " trisha should two monitors also michael .thanks kevin moore''',\n", " \n", " '''Subject: having problems in bed ? we can help !\n", " cialis allows men to enjoy a fully normal sex life without having to plan the sexual act .\n", " if we let things terrify us , life will not be worth living .\n", " brevity is the soul of lingerie . suspicion always haunts the guilty mind .''']\n", "\n", "cleaned_test = clean_text(emails_test)\n", "term_docs_test = cv.transform(cleaned_test)\n", "posterior = get_posterior(term_docs_test, prior, likelihood)\n", "from pprint import pprint\n", "pprint(posterior)\n", "# 검증결과 0번 메일은 0.98로 정상, 1번 메일은 0.99로 스펨에 해당" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **04-4 학습을 위해 Train / Test 데이터를 나눈다**\n", "scikit-learn 모듈 **train_test_split** 을 사용한다" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Counter({0: 3672, 1: 1500})" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from collections import Counter\n", "Counter(labels)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train 'email':3,465, 'label':3,465\n", "Test 'email':1,707, 'label':1,707\n" ] } ], "source": [ "from sklearn.model_selection import train_test_split\n", "X_train, X_test, Y_train, Y_test = train_test_split(\n", " cleaned_emails, # X_train, X_test 로 추출\n", " labels, # Y_train, Y_test 로 추출\n", " test_size = 0.33, \n", " random_state = 42)\n", "print(\"Train 'email':{:,}, 'label':{:,}\\nTest 'email':{:,}, 'label':{:,}\".format(\n", " len(X_train), len(Y_train), len(X_test), len(Y_test)))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/markbaum/Python/python/lib/python3.6/site-packages/ipykernel_launcher.py:26: RuntimeWarning: overflow encountered in exp\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "1,707 개의 테스트 데이터(Y_test)의 정확도는: 91.6 %\n" ] } ], "source": [ "# 데이터 Set의 사후 확률을 예측한다\n", "term_docs_train = cv.fit_transform(X_train)\n", "label_index = get_label_index(Y_train)\n", "prior = get_prior(label_index)\n", "likelihood = get_likelihood(term_docs_train, label_index, smoothing)\n", "\n", "# Test / 신규 데이터 Set의 사후확률을 예측한다\n", "term_docs_test = cv.transform(X_test)\n", "posterior = get_posterior(term_docs_test, prior, likelihood)\n", "correct = 0.0\n", "\n", "for pred, actual in zip(posterior, Y_test):\n", " if actual == 1:\n", " if pred[1] >= 0.5: correct += 1\n", " elif pred[0] > 0.5: correct += 1\n", "\n", "# dtype 을 128 이상으로 지정할 것\n", "# https://stackoverflow.com/questions/40726490/overflow-error-in-pythons-numpy-exp-function/40726641\n", "print('{:,} 개의 테스트 데이터(Y_test)의 정확도는: {:.1f} %'.format(\n", " len(Y_test), correct/len(Y_test)*100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## **5 Sklearn 을 활용한 Naive Bayse 구현하기**\n", "- 위에서 복잡한 과정을 sklearn으로 실습 합니다\n", "- nltk 모듈을 활용한 예제 [nltk_tutorial](https://nbviewer.jupyter.org/github/YongBeomKim/nltk_tutorial/blob/master/ipython/03-2.Bayse.ipynb)\n", "### **01 데이터 전처리 및 모델학습**\n", "모델을 학습한 뒤 정확도를 측정한다\n", "```python\n", "# cleaned_emails[0] : 전처리된 텍스트 List\n", "from sklearn.feature_extraction.text import CountVectorizer\n", "cv = CountVectorizer(stop_words=\"english\", max_features=500)\n", "term_docs_test = cv.transform(cleaned_test)\n", "```" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[9.99808489e-01, 1.91511166e-04],\n", " [9.99999772e-01, 2.28176513e-07],\n", " [9.99999223e-01, 7.77402015e-07],\n", " [9.99999724e-01, 2.76311984e-07],\n", " [9.98447799e-01, 1.55220148e-03],\n", " [1.00000000e+00, 2.17331050e-15]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.naive_bayes import MultinomialNB\n", "clf = MultinomialNB(alpha = 1.0, # 라플라스 Smoothing 값\n", " fit_prior = True) # Data Set로 학습된 사전확률 사용\n", "clf.fit(term_docs_train, Y_train)\n", "prediction_prob = clf.predict_proba(term_docs_test)\n", "prediction_prob[0:6]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 예측한 클래스 값을 바로 계산하여 출력한다\n", "# 역치값은 0.5로 0.5보다 크면 1, 작으면 0을 출력\n", "prediction = clf.predict(term_docs_test)\n", "prediction[:10]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The accuracy using MultinomialNB is: 91.6%\n" ] } ], "source": [ "# test 값을 활용하여 모델의 정확도 측정 \n", "accuracy = clf.score(term_docs_test, Y_test)\n", "print('The accuracy using MultinomialNB is: {0:.1f}%'.format(accuracy*100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **02 분류기의 성능 평가**\n", "**혼동행렬(Confusion Matrix) 분할표로** 예측값을 테스트하여 출력한다\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1138, 91],\n", " [ 52, 426]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 혼동행렬을 계산\n", "from sklearn.metrics import confusion_matrix\n", "confusion_matrix(Y_test, prediction, labels=[0, 1])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Precesion(정밀도) : 0.824\n", "Recall(재현율) : 0.8912\n", "f1 score (1) : 0.8563 \n", "f1 score (0) : 0.9409\n" ] } ], "source": [ "# f1 Score 를 측정하여 정밀도, 재연율을 계산\n", "from sklearn.metrics import precision_score, recall_score, f1_score\n", "print(\"\"\"Precesion(정밀도) : {:.4}\\nRecall(재현율) : {:.4}\n", "f1 score (1) : {:.4} \\nf1 score (0) : {:.4}\"\"\".format(\n", " precision_score(Y_test, prediction, pos_label=1),\n", " recall_score(Y_test, prediction, pos_label=1),\n", " f1_score(Y_test, prediction, pos_label=1),\n", " f1_score(Y_test, prediction, pos_label=0)))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " precision recall f1-score support\n", "\n", " 0 0.96 0.93 0.94 1229\n", " 1 0.82 0.89 0.86 478\n", "\n", " micro avg 0.92 0.92 0.92 1707\n", " macro avg 0.89 0.91 0.90 1707\n", "weighted avg 0.92 0.92 0.92 1707\n", "\n" ] } ], "source": [ "# 위 내용을 한꺼번에 실행해본다\n", "from sklearn.metrics import classification_report\n", "report = classification_report(Y_test, prediction)\n", "print(report)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### **03 분류기의 성능 평가**\n", "1. **정확도**(훈련데이터 적합도) 와 **재현율**(일반화 정도)이 **모두 높은 경우가 없기** 때문에 f1-score를 측정한다\n", "1. 하지만 모델의 **평균값과,** 모델의 **f1-score** 둘 다 높은 모델은 없으므로 별도 기준이 필요\n", "1. 대표적인 대안으로 **ROC (Receiver Operation Characteristic), AUC (Area Under the Curve)** 가 있다\n", "1. 이번 예제에서는 **ROC**를 그려보자" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 2.8 ms, sys: 0 ns, total: 2.8 ms\n", "Wall time: 2.77 ms\n" ] } ], "source": [ "%%time\n", "# ROC Curve 값들을 계산합니다\n", "pos_prob = prediction_prob[:, 1]\n", "thresholds = np.arange(0.0, 1.2, 0.1)\n", "true_pos = [0]*len(thresholds) \n", "false_pos = [0]*len(thresholds)\n", "\n", "for pred, y in zip(pos_prob, Y_test):\n", " for i, threshold in enumerate(thresholds):\n", " if pred >= threshold:\n", " if y == 1: true_pos[i] += 1\n", " else: false_pos[i] += 1\n", " else: break\n", "\n", "# 임계치를 설정하기 위해 양성비율과, 음성 비율을 계산한다\n", "# 양성 테스트 샘플이 516개, 음성 테스트 샘플이 1,191개 이다\n", "true_pos_rate = [tp / 516.0 for tp in true_pos]\n", "false_pos_rate = [fp / 1191.0 for fp in false_pos]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYoAAAEWCAYAAAB42tAoAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xd4VGX2wPHvSU/oJIBIF5AiIGhEFAUUBQRs2EDFhgURFfGH6KJi3VVUFKUE27rKuhZ2VQREUUFsKKiAgoA0IUiH0BNSzu+PewNjTCaTkJk7k5zP8+SZuf3Mzcyced/33vcVVcUYY4wpSpTXARhjjAlvliiMMcb4ZYnCGGOMX5YojDHG+GWJwhhjjF+WKIwxxvhlicIETESuEpFPvI4jnIjIPhE5zoPjNhYRFZGYUB87GERkqYh0K8V29p4MAUsUEUpE1onIQfeLarOIvCYilYN5TFX9t6r2COYxfInI6SLyuYjsFZHdIvKhiLQO1fELiWeuiNzoO09VK6vqmiAd73g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ShqjtDGchRsfaJkKtTpxINGlQjJiaKSZP60LVrY69DMsYESfnoec0SRdDl5SkvvfQDZ53VhOOPT0ZEePPNftSokUhcXLTX4Rljgqh83PVkvcYG1eLFm+nc+VUGD57BkCEzyO8Xsk6dypYkjKkAIr9EoWptFEGyb98hHnpoLs89N5/cXOXYY6sweHCq12EZY0Is8hPF/k2QtRsSakJSba+jKTfef385t9/+Eenpe4iKEm6/vSOPPXY2VavGex2aMSbEIj9R+LZP2CWZZWLjxj307z+VrKxcTj65LmlpfUlNPdbrsIwxHilficKUWnZ2LjExUYgI9epV5fHHzyYuLpohQ06xMauNqeAi/xvAEsVR++abDZx88otMmbLk8Ly77z6d228/1ZKEMaYcJQrruqPEdu48yC23fEjnzq/y889bmThxIeE80q0xxhuRX/WUscp5rGkdzwVKVZkyZQl33/0J27YdIDY2invu6cyoUWda1xvGmL+I7ESRlwsHtjrPK9X1NpYIsWXLPgYM+C9z5qwDoGvXRkya1IdWrWp5G5gxJmxFdqI4uB00FxKSITrO62giQvXqCWzatI+UlCSefvpcrrnmRCtFGGP8iuxEsX+T81jZShP+zJ69mpNOqktychLx8TG8++5l1K1bmeRk68DPGFO8yG7M3r/ZeUw6xts4wtSmTXsZMOC/9OgxhZEjPz08v02b2pYkjDEBsxJFOZSbm8fkyT9w332fsWdPFomJMbRokWyDCRljSqV8JApryD7sxx83MXjwdBYs+AOAPn2aM358bxo3ru5xZMaYSBXhicKteqpkVU8A69Zl0LHjS+TmKvXqVeH558/j4otbWinCGHNUgpooRKQXMA6IBl5W1ScKLB8O3AjkANuAG1T194APYCWKP2ncuDrXX9+eKlXiefjhblSpYh34GWOOXtAas0UkGpgAnAe0BgaISMHbp38CUlW1HTAVGFOig+yr2Ili3boMzj//P3zxxbrD81588XzGju1pScIYU2aCWaLoCKxS1TUAIvIWcCGwLH8FVZ3js/584OoSHeFAxax6ys7OZezYb3n44S84eDCH7dsP8O23gwCsmskYU+aCmSjqARt8ptOBU/2sPwj4qLAFInIzcDNAw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iiRqr4pIt8BfYCZInILzkhe/1LV+wI4xlW+HQiKSM3CVnL7FuqI08ncpcBQ4OwSvJa3gMuB5cB7qqrifGsHHCfwA077xAtAPxFpAvwfcIqq7hKR13A6vitIgNmqOqAE8ZoKzqqeTKSoBmxyxw8YiNP525+IyHHAGre65QOcKpjPgEtFpLa7Tk0JfEzxFUBjEWnmTg8EvnDr9Kup6kycBHZiIdvuxen2vDDv4Yw0NgAnaVDSON0O7R4AOolIS5zR2/YDu0WkDnBeEbHMBzrnvyYRqSQihZXOjDnMEoWJFBOBa0VkMU51zf5C1rkc+EVEFuGMS/G6e6XR/cAnIrIEmI1TLVMsVc3E6V3zXRH5GcgD0nC+dKe7+/uKwuv4XwPS8huzC+x3F/Ar0EhVv3fnlThOt+3jGZxeYRfjjI+9HHgTpzor34vALBGZo6rbcK7I+o97nG9xzqcxRbLeY40xxvhlJQpjjDF+WaIwxhjjlyUKY4wxflmiMMYY45clCmOMMX5ZojDGGOOXJQpjjDF+/T/i7g2Hxo9dqwAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "%matplotlib inline\n", "# ROC Curve 를 출력한다\n", "import matplotlib.pyplot as plt\n", "lw = 2 # BaseLine을 그린다\n", "plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')\n", "plt.plot(false_pos_rate, true_pos_rate, color = 'darkorange', lw = lw)\n", "plt.xlim([0.0, 1.0])\n", "plt.ylim([0.0, 1.05])\n", "plt.xlabel('False Positive Rate')\n", "plt.ylabel('True Positive Rate')\n", "plt.title('Receiver Operating Characteristic')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9629610085418291" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.metrics import roc_auc_score\n", "roc_auc_score(Y_test, pos_prob)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## **6 Sklearn 을 활용한 모델의 튜닟 및 교차검증**\n", "1. 모델이 실질적으로 잘 작동하는지 **K-fold 검정을** 적용한다\n", "1. **AUC 값의** 측정 : **ROC 커브의** 밑면적을 구한 값으로 **1에 가까울수록** 성능이 좋다.[참고](http://newsight.tistory.com/53)\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "# 전체 10개의 폴드 생성기로 초기화 후 파라미터 분석을 진행합니다\n", "from sklearn.model_selection import StratifiedKFold\n", "k = 10\n", "k_fold = StratifiedKFold(n_splits=k)\n", "\n", "# 연산을 위해 Numpy 객체로 변환한다\n", "cleaned_emails_np = np.array(cleaned_emails)\n", "labels_np = np.array(labels)\n", "\n", "# 10 폴드 생성기 학습을 위한 파라미터를 정의합니다\n", "max_features_option = [2000, 4000, 8000] # 가장 많이 사용되는 N개 단어를 선택\n", "smoothing_factor_option = [0.5, 1.0, 1.5, 2.0] # Smoothing Parameter : 초기값\n", "fit_prior_option = [True, False] # 사전 확률을 사용할지 여부" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 24.8 s, sys: 1.16 s, total: 26 s\n", "Wall time: 26 s\n" ] } ], "source": [ "%%time\n", "auc_record = {} # k_fold 분리된 객체를 활용하여 개별 환경에서 AUC를 측정\n", "for train_indices, test_indices in k_fold.split(cleaned_emails, labels):\n", " X_train, X_test = cleaned_emails_np[train_indices], cleaned_emails_np[test_indices]\n", " Y_train, Y_test = labels_np[train_indices], labels_np[test_indices]\n", "\n", " # max_features_option 환경값을 바꿔가면서 AUC 테스트\n", " for max_features in max_features_option: \n", " if max_features not in auc_record:\n", " auc_record[max_features] = {}\n", " cv = CountVectorizer(stop_words=\"english\", max_features=max_features)\n", " term_docs_train = cv.fit_transform(X_train)\n", " term_docs_test = cv.transform(X_test)\n", " \n", " # smoothing_factor_option 초기값을 바꾸며 AUC 테스트\n", " for smoothing_factor in smoothing_factor_option:\n", " if smoothing_factor not in auc_record[max_features]:\n", " auc_record[max_features][smoothing_factor] = {}\n", " \n", " # fit_prior_option : 사전확률을 바꾸며 AUC 테스트\n", " for fit_prior in fit_prior_option:\n", " clf = MultinomialNB(alpha=smoothing_factor, fit_prior=fit_prior)\n", " clf.fit(term_docs_train, Y_train)\n", " prediction_prob = clf.predict_proba(term_docs_test)\n", " pos_prob = prediction_prob[:, 1]\n", " auc = roc_auc_score(Y_test, pos_prob)\n", " auc_record[max_features][smoothing_factor][fit_prior] \\\n", " = auc + auc_record[max_features][smoothing_factor].get(fit_prior, 0.0) \n", "\n", "# 위에서 계산한 결과를 출력합니다\n", "auc_result = []\n", "for max_features, max_feature_record in auc_record.items():\n", " for smoothing, smoothing_record in max_feature_record.items():\n", " for fit_prior, auc in smoothing_record.items():\n", " auc_result.append([max_features, smoothing, fit_prior, auc/k])" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
max featuressmoothingfit priorauc
080000.5True0.988589
180000.5False0.988520
280001.0True0.987575
380001.0False0.987436
480001.5True0.987039
\n", "
" ], "text/plain": [ " max features smoothing fit prior auc\n", "0 8000 0.5 True 0.988589\n", "1 8000 0.5 False 0.988520\n", "2 8000 1.0 True 0.987575\n", "3 8000 1.0 False 0.987436\n", "4 8000 1.5 True 0.987039" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "auc_result = pd.DataFrame(auc_result)\n", "auc_result.columns = ['max features', 'smoothing', 'fit prior', 'auc']\n", "auc_result = auc_result.sort_values('auc', ascending=False).reset_index(drop=True)\n", "auc_result.head()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.9" } }, "nbformat": 4, "nbformat_minor": 4 }