250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- psycopg2
- 리눅스
- 연결
- Memory
- GPU
- TensorFlow
- 도커이미지
- jupyternotebook
- GridSearchCV
- 교차검증
- pgadmin
- Docker image
- 시계열
- psql
- sqldeveloper
- 파이썬
- Python
- 머신러닝
- docker
- SQL
- oracle
- 오라클
- Jupyter
- 쿼리
- 도커
- LOG
- cpu
- postgre
- 복구
- Linux
Archives
- Today
- Total
areum
[NLP] 텍스트 카테고리 분류하는 방법 본문
728x90
파이썬으로 텍스트 카테고리 분류해보기
1. 기본 세팅
데이터는 https://github.com/e9t/nsmc에서 받을 수 있습니다.
(위 데이터는 네이버 영화 평점 데이터입니다.)
import pandas as pd
import numpy as np
from tqdm import tqdm
# 시각화
import matplotlib.pyplot as plt
df = pd.read_csv('/content/ratings_train.txt',sep='\t')
# 데이터 양이 너무 많아 200행까지만 실행해보았습니다.
df=df.iloc[0:200,0:2]
2. 긍정/부정/중립의 키워드 지정 후 카테고리화 하였습니다.
긍정과 부정의 키워드는 제가 임의로 작성한 키워드 입니다.
# 긍정 키워드
pos_words = '좋아 좋아요 좋은 좋았 짱 정말 재미 재밋 재밌 재미있다 추천 사랑 감성 또 마음 맘 멋진 멋지다 웃기다 웃김 기다렸다 절대 센스 최고 열정'
# 부정 키워드
neg_words = '허접 짜증 재미없다 별로다 별로 안본다 안볼듯 없다 낭비 아깝다 아까움 노잼 발연기 가볍'
positive_words = pos_words.split(' ')
negative_words = neg_words.split(' ')
# 긍정 채팅 분류
for i in tqdm(df.index):
for word in positive_words:
if word in df.loc[i, 'document']:
df.loc[i, '감정분석'] = '긍정'
# 부정 채팅 분류
for i in tqdm(df.index):
if df.loc[i, '감정분석']!='긍정':
for word in negative_words:
if word in df.loc[i, 'document']:
df.loc[i, '감정분석'] = '부정'
# 중립
df['감정분석'] = df['감정분석'].fillna('중립')
df.head(50)
3. 긍정/부정/중립의 개수 확인
df2 = df['감정분석'].value_counts()
df2
4. 긍정/부정/중립의 비율 시각화로 표현해보았습니다.
import matplotlib.pyplot as plt
plt.rc('font',family='NanumBarunGothic', size=20)
plt.figure(figsize=(8, 8))
ratio = df2.values
labels = df2.index
colors=['#ff9999', '#ffc000', '#8fd9b6']
wedgeprops = {'width': 0.7,'linewidth': 2}
explode = [0.05, 0.05, 0.05]
plt.pie(ratio, autopct='%.1f%%',startangle=90, counterclock=False, labels=labels, colors=colors, explode=explode)
plt.show()
* 긍정과 부정의 단어가 명확하게 나누어진 것이 아니여서 중립이 비율이 높게 나온 거 같습니다.
'Programming > NLP' 카테고리의 다른 글
[NLP] 형태소 분석하기(feat. KoNLPy) (0) | 2023.03.30 |
---|---|
[NLP] 텍스트 Emoji(이모티콘) 제거하는 방법 (1) | 2023.03.29 |