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 | 31 |
Tags
- ๋ฐฐ๋ฌ์๋ฏผ์กฑ
- ์น_ํ๋ก๊ทธ๋๋ฐ #์น์_๊ธฐ๋ณธ
- ์ฐ์์ฝ
- ์ค๋ธ์
- clean_code
- it์ปจํผ๋ฐ์ค
- ์๋ฃ๊ตฌ์กฐ
- ๋ค์ง #์ฒซ_์์ #์ด๋ณด๋ธ๋ก๊ฑฐ
- CS์ง์
- 2024woowacon
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- 2024
Archives
- Today
- Total
Data house
๋ค์ด๋ฒ ์ํ ํ์ ๊ฐ์ฑ ๋ถ์ - ํ๊ธ ํ ์คํธ ์ฒ๋ฆฌ ๋ณธ๋ฌธ
Data Science/Machine Learning
๋ค์ด๋ฒ ์ํ ํ์ ๊ฐ์ฑ ๋ถ์ - ํ๊ธ ํ ์คํธ ์ฒ๋ฆฌ
l._.been 2023. 7. 31. 19:21728x90
์ค๋น ๐๐ปโ๏ธ
- ์ ๊ฐ: ๋ฐ์ดํฐ EDA -> ๋ฐ์ดํฐ ์ ์ (๊ฒฐ์ธก์น/ ์ด์์น) -> TF-IDF -> ๋ชจ๋ธ๋ง -> ์์ธก
- ์ฌ์ฉํ ๋ฐ์ดํฐ : https://github.com/e9t/nsmc
GitHub - e9t/nsmc: Naver sentiment movie corpus
Naver sentiment movie corpus. Contribute to e9t/nsmc development by creating an account on GitHub.
github.com
๋ฐ์ดํฐ EDA
1. jupyter notebook์ ๋ค์ดํ ๋ฐ์ดํฐ ๋ก๋
- read_csv : ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋ ์ฃผ์๋ฅผ ๊ธฐ์
- sep: ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ ( tab์ผ๋ก ๋๋ ์ ธ ์์ผ๋ฉด '\t')
import warnings
warnings.filterwarnings(action = 'ignore')
import pandas as pd
train_df = pd.read_csv('../data/nsmc/ratings_train.txt', sep='\t')
train_df.head(3)
2. ๊ธ๋ถ์ ๋ฐ์ดํฐ์ ์ ์์ ๋น๊ตํ๊ธฐ
- ๊ธ๋ถ์ ๋ฐ์ดํฐ์ ์์ด ๋น์ทํ์ง check --> ๋ง์ฝ ๋ฐ์ดํฐ ๋ถ๊ท ํ์ด ์๋ค๋ฉด oversampling / undersampling ๊ณ ๋ คํด์ผ ํจ
- value_counts: ํด๋น ์ด์ ์ด๋ค ๋ฐ์ดํฐ๊ฐ ์๊ณ ๋ช๊ฐ๊ฐ ์๋์ง ํ์ธํ ์ ์์
- ๊ฒฐ๊ณผ ๊ฐ์ ๋ณด์ํ๋, ๊ธ์ ๋ฐ์ดํฐ(1)์ ๋ถ์ ๋ฐ์ดํฐ(0)๊ฐ ๊ฑฐ์ 5:5๋น์จ๋ก ์ ์์์ ํ์ธํ ์ ์๋ค.
# ํ์ต๋ฐ์ดํฐ์ ๋ ์ด๋ธ
train_df['label'].value_counts()
๋ฐ์ดํฐ ์ ์ (๊ฒฐ์ธก์น / ์ด์์น)
1. null๊ฐ ์ฌ๋ถ(๊ฒฐ์ธก์น) ํ์ธํ๊ธฐ
- info: ๋ฐ์ดํฐ ํ๋ ์์ ์ด์ ์ด๋ฆ๊ณผ ํด๋น ํ์ ๋ฐ์ดํฐ ํ์ ๊ณผ null์ ์ฌ๋ถ๋ฅผ ํ์ธ ๊ฐ๋ฅ
- ์๋ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋, document ์ด์ 5๊ฐ์ null๊ฐ์ด ์กด์ฌํ๋ค.
# document์ 5๊ฐ์ null ๊ฐ์ด ์์
train_df.info()
2. document ์ด์ ์ซ์ ์ญ์ ํ๊ธฐ
- ์๋ ์ฝ๋์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ์ฌ๋์ ๊ฐ์ ์ ํ๋ณํ๋๋ฐ ๊ด๋ จ์ฑ์ด ์๋ ์ซ์๋ค์ด ๋ฐ์ดํฐ์ ํฌํจ๋์ด์๋ ๊ฒ์ผ๋ก ํ์ธ๋์๊ธฐ ๋๋ฌธ์ ์ญ์ ํด์ผํ๋ค!
# set_option(์ต์
์ค์ ) - head ์
๋ ฅ์ ๋ฐ์ดํฐ ํ๊ณผ ์ด์ ์ต๋ ์ถ๋ ฅ๋ ์กฐ์ ํ๊ธฐ
pd.set_option('display.max_row', 500)
pd.set_option('display.max_columns', 100)
print(train_df['document'].head(100))
- fillna(' ') : ๊ฒฐ์ธก์น๋ฅผ ' '(๊ณต๋ฐฑ)์ผ๋ก ์ฒ๋ฆฌ ํจ
- re : regex(์ ๊ท ํํ์) ๋ด์ฅ ๋ชจ๋
- re.sub(r'\d+', ' ' , x): ๋ฌธ์์ด์์ '\d+' (์ซ์)๋ฅผ ' '(๊ณต๋ฐฑ)์ผ๋ก ์ฒ๋ฆฌํจ
- drop('id', axis= ? , inplace = ? ): id ์นผ๋ผ์ ์ญ์ ํ๋๋ฐ axis= 1์ด๋ฉด ์ด์ ์ญ์ ํ๊ณ , axis=0์ด๋ฉด ํ์ ์ญ์ ํ๋ค. inplace๊ฐ True์ด๋ฉด drop์ ์ ์ฉํ dataframe ๊ฒฐ๊ณผ๊ฐ์ผ๋ก ๋ฐํํ๊ณ inplace๊ฐ False์ด๋ฉด ์๋ณธ๋ฐ์ดํฐ์ drop์ ๋ฐ์ํ์ง ์๋๋ค.
import re
train_df = train_df.fillna(' ')
# re - ์ซ์๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๋ณ๊ฒฝ
train_df['document'] = train_df['document'].apply(lambda x : re.sub(r'\d+', ' ', x))
# re - ๋ฆฌ๋ทฐ์ Null ๋ฐ ์ซ์๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๋ณํ
# \d+ - 1๊ฐ ์ด์์ ์ซ์๊ฐ ๋์ค๋ฉด ๊ณต๋ฐฑ์ผ๋ก ๋ณ๊ฒฝ
test_df = pd.read_csv('../data/nsmc/ratings_test.txt', sep='\t')
test_df = test_df.fillna(' ')
test_df['document'] = test_df['document'].apply(lambda x: re.sub(r'\d+', ' ', x))
# id ์นผ๋ผ ์ญ์
train_df.drop('id', axis=1, inplace=True)
test_df.drop('id', axis=1, inplace=True)
TF-IDF ์ฌ์ฉ
1. konlpy ์ฌ์ฉํด์ ํํ์ ๋ถ๋ฆฌํ๋ ํจ์ ๋ง๋ค๊ธฐ & TF-IDF
- tw_tokenizer(): morphs(๋ช ์ฌ)๋ก tokenizer ํ๋ ํจ์
from konlpy.tag import Twitter
twitter = Twitter()
def tw_tokenizer(text):
#์
๋ ฅ ๋ค์ด์จ text๋ฅผ ํํ์ ๋จ์ด๋ก ํ ํฐํํ์ฌ list๊ฐ์ฒด ๋ณํ
tokens_ko = twitter.morphs(text)
return tokens_ko
tw_tokenizer('์ค๋์ ๋ ์จ๊ฐ ์ข์ต๋๋ค')
- TfidfVectorizer: TF-IDF๋ฅผ ๊ฐ๋ฅํ๊ฒํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
# ngram_range = (1,2) : go back, go home
# ngram_range = (1,1) : go, back, home
# min_df = 3 : ์ต์ ๋น๋๊ฐ์ ์ค์ , df = ๋ฌธ์์ ์
# max_df = 0.9 == 90% ์ด์ ๋ํ๋๋ ๋จ์ด๋ ๋ฌด์ํ๊ฒ ๋ค. (10๊ฐ ๋ฌธ์ฅ์์ 9๋ฒ ์ด์ ๋์ค๋ ๋จ์ด ๋ฌด์)
tfidf_vect = TfidfVectorizer(tokenizer = tw_tokenizer, ngram_range = (1,2), min_df=3, max_df=0.9)
tfidf_vect.fit(train_df['document'])
tfidf_matrix_train = tfidf_vect.transform(train_df['document'])
print(tfidf_matrix_train.shape)
๋ชจ๋ธ๋ง
1. ๋ถ๋ฅ ๋ชจ๋ธ (LogisticRegression) & GridSearchCV ์ฌ์ฉํ์ฌ ํ์ดํผํ๋ผ๋ฏธํฐ ์ต์ ํ
- ๋ฒ ์ด์ค ๋ชจ๋ธ๋ก Logistic Regression์ ์ฌ์ฉํ๊ณ ๋ชจ๋ธ์ ์ฑ๋ฅ์ ์ต๋์น๋ก ๋ง์ถ๊ณ ์ GridsearchCV๋ฅผ ์ฌ์ฉํด์ ์ต์ ์ ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ป๊ณ ์ ํ๋ค.
- C: ๊ท์ ๋ฅผ ์ ํ๋ ํ๋ผ๋ฏธํฐ์ธ๋ฐ GridSearchCV์ ๋ฃ์ ๊ฒฝ์ฐ ๊ฐ์ฅ ๊ฒฐ๊ณผ๊ฐ ์ข์๋ ๊ฐ์ด ๋ฐํ๋๋ค
- ์๋ ๊ฒฐ๊ณผ์์๋ C๊ฐ 4.5์ผ๋ 0.8922 ์ ํ๋๋ฅผ ๋ณด์๋ค.
# Logistic Regression ์ด์ฉ ๊ฐ์ฑ ๋ถ์ Classification ์ํ
lg_clf = LogisticRegression(random_state = 0)
# Parameter C ์ต์ ํ - GridSearchCV
params = {'C': [1, 3.5, 4.5, 5.5, 10]}
grid_cv = GridSearchCV(lg_clf, param_grid=params, cv= 3, scoring='accuracy', verbose = 1)
grid_cv.fit(tfidf_matrix_train, train_df['label'])
print(grid_cv.best_params_, round(grid_cv.best_score_, 4))
2. test ๋ฐ์ดํฐ TF-IDFํ ํ๊ธฐ & ๋ชจ๋ธ ์ ํ๋ ์ธก์
- accuracy_score: ์ ํ๋
- ์๋ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ํ๋, GridSearchCV๋ก ์ป์ ์ต์ ์ ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ฉํ Logistic Regression์ ์ ํ๋๋ ์ฝ 86% ์ด๋ค.
from sklearn.metrics import accuracy_score
# ํ์ต ๋ฐ์ดํฐ๋ฅผ ์ ์ฉํ TfidfVectorizer๋ฅผ ์ด์ฉ
# ํ
์คํธ ๋ฐ์ดํฐ๋ฅผ TF-IDF ๊ฐ์ผ๋ก Feature ๋ณํ
tfidf_matrix_test = tfidf_vect.transform(test_df['document'])
# ์ต์ ํ๋ผ๋ฏธํฐ๋ก ํ์ต๋ classifier๋ฅผ ๊ทธ๋๋ก ์ด์ฉ
best_estimator = grid_cv.best_estimator_
preds = best_estimator.predict(tfidf_matrix_test)
print('Logistic Regression ์ ํ๋: ', accuracy_score(test_df['label'], preds) )
์์ธก
1. ๋ชจ๋ธ๋ง ํ๊ฐ - ๊ธฐ์กด ๋ฐ์ดํฐ ์ฌ์ฉ
- document์ 150๋ฒ์งธ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํด์ ๋ชจ๋ธ์ ํ๊ฐํด๋ณธ ๊ฒฐ๊ณผ, ๊ธ์ (1)์ผ๋ก ์ ๋ถ๋ฅํ๊ณ ์๋ค.
test_df['document'][150]
best_estimator.predict(tfidf_vect.transform([test_df['document'][150]]))
- document์ 2๋ฒ์งธ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํด์ ๋ชจ๋ธ์ ํ๊ฐํด๋ณธ ๊ฒฐ๊ณผ, ๋ถ์ (0)์ผ๋ก ์ ๋ถ๋ฅํ๊ณ ์๋ค.
test_df['document'][2]
best_estimator.predict(tfidf_vect.transform([test_df['document'][2]]))
2. ๋ชจ๋ธ๋ง ํ๊ฐ - ๋ด๊ฐ ๋ง๋ ๋ฐ์ดํฐ ์ฌ์ฉ
- ๊ฒฐ๊ณผ๊ฐ ์ฌ๋ฐ๊ฒ ๋์๋ค ใ ใ ๋ด ์๊ฐ๋๋ก ๋ชจ๋ธ์ด ๊ธ๋ถ์ ์ ์ ํ๋จํด๊ณ ์๋ค ใ ใ ใ
- ์ผ๋ถ๋ฌ ๊ฐ์ ์ ํ๋จํ์ง ๋ชปํ๊ฒ ๊ผฌ์์ ๋๋๋ฐ ๋๋ฆ ๊ด์ฐฎ์ ํ๋จ์ด ๋์์ ๋๋๋ค ใ ใ ใ
text = '์ํ ์ฌ๋ฏธ์๋ค์'
text2 = '์ํ๊ฐ ๋ด ์คํ์ผ์ ์๋์์ง๋ง ๋ฐฐ์ฐ๊ฐ ์ฐ๊ธฐ๋ฅผ ์ ์ด๋ ค์ ๋ณผ๋ง ํ๋ค'
text3 = '๐'
if best_estimator.predict(tfidf_vect.transform([text])) == 0:
print(f'"{text}" -> ๋ถ์ ์ผ ๊ฐ๋ฅ์ฑ์ด {round(best_estimator.predict_proba(tfidf_vect.transform([text]))[0][0], 2)*100}% ์
๋๋ค')
else:
print(f'"{text}" -> ๊ธ์ ์ผ ๊ฐ๋ฅ์ฑ์ด {round(best_estimator.predict_proba(tfidf_vect.transform([text]))[0][1], 2)*100}% ์
๋๋ค')
if best_estimator.predict(tfidf_vect.transform([text2])) == 0:
print(f'"{text2}" -> ๋ถ์ ์ผ ๊ฐ๋ฅ์ฑ์ด {round(best_estimator.predict_proba(tfidf_vect.transform([text2]))[0][0], 2)*100}% ์
๋๋ค')
else:
print(f'"{text2}" -> ๊ธ์ ์ผ ๊ฐ๋ฅ์ฑ์ด {round(best_estimator.predict_proba(tfidf_vect.transform([text2]))[0][1], 2)*100}% ์
๋๋ค')
if best_estimator.predict(tfidf_vect.transform([text3])) == 0:
print(f'"{text3}" -> ๋ถ์ ์ผ ๊ฐ๋ฅ์ฑ์ด {round(best_estimator.predict_proba(tfidf_vect.transform([text3]))[0][0], 2)*100}% ์
๋๋ค')
else:
print(f'"{text3}" -> ๊ธ์ ์ผ ๊ฐ๋ฅ์ฑ์ด {round(best_estimator.predict_proba(tfidf_vect.transform([text3]))[0][1], 2)*100}% ์
๋๋ค')
3. ๋ชจ๋ธ ์์ธก ๊ฒฐ๊ณผ
- ์๊น TF-IDF ํ๋ test_df์ ๋ชจ๋ธ์ ๋ฃ์ด ์ป์ ๊ฒฐ๊ณผ ๊ฐ๋ค์ ์์๊ฒ dataframe์ผ๋ก ์ ๋ฆฌํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค
result = pd.DataFrame({
'document': test_df['document'],
'test': test_df['label'],
'pred': preds
})
result
'Data Science > Machine Learning' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
CNN๊ณผ OpenCV๋ฅผ ํ์ฉํ ๋ง์คํฌ ๊ฐ์ง ๋ชจ๋ธ (0) | 2023.08.23 |
---|---|
streamlit - ๋ฐ์ดํฐ๋ถ์ /ML์ ์ํ ์น์ฑ (0) | 2023.08.02 |