-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_TextPreProcessing.py
More file actions
61 lines (33 loc) · 1.72 KB
/
Copy path2_TextPreProcessing.py
File metadata and controls
61 lines (33 loc) · 1.72 KB
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
# -*- coding: utf-8 -*-
# 머신러닝에서 문자 형태로 저장된 특성 데이터를
# 사용하여 학습하는 방법
# 1. 문자형의 데이터를 수치데이터로 변경
# - 문자열로 구성된 각 단어를 구분하여 라벨 값을 지정
# - 문자열을 구성하고 있는 각 단어의 빈도수를 계산
# - 문자열의 형태를 각 단여의 빈도수로 변경
# 2. 각 문서(말뭉치를 구성하는 각 샘플 데이터)를 토큰 개수
# 만큼의 행렬로 표현
# - 각 문서를 구성하고 있는 토큰의 개수를 카운팅하여
# - 0 또는 개수를 출력
# (문서 데이터를 서로 다른 길이를 가지므로, 모든 문서데이터가
# 동일한 크기의 특성을 가지도록 강제하는 방법)
# CountVectorizer 클래스
# 문서(하나 또는 다수개의 텍스트 문장) 집합에서
# 단어들의 토큰을 생성하고, 각 단어의 수를 카운팅하여
# BOW 타입으로 인코딩 된 벡터를 생성하는 클래스
from sklearn.feature_extraction.text import CountVectorizer
corpus = ['Hello Python',
'Hello Scikit learn',
'This is first document',
'This is second document',
'This is third document',
'The last document']
vectorizer = CountVectorizer().fit(corpus)
print('토큰(단어)의 개수 : ', len(vectorizer.vocabulary_))
print('토큰(단어)의 내용 : \n', vectorizer.vocabulary_)
print('변환 결과(희소행렬) : \n',
vectorizer.transform(['This is second second document']))
print('변환 결과(행렬) : \n',
vectorizer.transform(['This is second second document']).toarray())
print('변환 결과(말뭉치변환) : \n',
vectorizer.transform(corpus).toarray())