์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- opencv
- pandas
- ๋น ๋ฐ์ดํฐ
- ํ์ด์ฌ
- ์ค๋ฒ์ํ๋ง
- ADsP
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- Python
- ์๋ํด๋ผ์ฐ๋
- Lambda
- iloc
- ๋ฐ์ดํฐ๋ถ์
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- ํฌ๋กค๋ง
- numpy
- t-test
- datascience
- ๊ตฐ์งํ
- ์ธ๋์ํ๋ง
- LDA
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- DBSCAN
- ํ ์คํธ๋ถ์
- PCA
- dataframe
- ๋์ํ๋ณธ
- ๋ ๋ฆฝํ๋ณธ
- ์ฃผ์ฑ๋ถ๋ถ์
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- ADP
Data Science LAB
[Python] ๊ฐ์ฑ ๋ถ์(Sentiment Analysis) - ์ง๋ํ์ต ๋ณธ๋ฌธ
[Python] ๊ฐ์ฑ ๋ถ์(Sentiment Analysis) - ์ง๋ํ์ต
ใ ใ ใ ใ 2022. 2. 19. 15:10๊ฐ์ฑ๋ถ์ ์ด๋?
๊ฐ์ฑ๋ถ์์ด๋ ๋ฌธ์์ ์ฃผ๊ด์ ์ธ ๊ฐ์ฑ/์๊ฒฌ/๊ฐ์ /๊ธฐ๋ถ ๋ฑ์ ํ์ ํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ผ๋ก, ์์ ๋ฏธ๋์ด๋ ์ฌ๋ก ์กฐ์ฌ, ์จ๋ผ์ธ ๋ฆฌ๋ทฐ ๋ฑ ๋ค์ํ ๋ถ์ผ์์ ํ์ฉ๋๊ณ ์๋ค. ๋ฌธ์์ ๊ธ์๊ฐ ๋ํ๋ด๋ ์ฌ๋ฌ ์ฃผ๊ด์ ์ธ ๋จ์ด์ ๋ฌธ๋งฅ์ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ฑ ์์น๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ์ด์ฉํ๋ค. ๊ฐ์ฑ ์์น๋ฅผ ๊ธ์ /๋ถ์ ์ง์๋ก ๊ตฌ๋ถ์ง์ด ๊ฐ ์ง์๋ฅผ ํฉ์ฐํ์ฌ ๊ธ์ ๋๋ ๋ถ์ ๊ฐ์ฑ์ ๊ฒฐ์ ํ๋ค.
๊ฐ์ฑ๋ถ์์ ํฌ๊ฒ ์ง๋ํ์ต๊ณผ ๋น์ง๋ ํ์ต ๋ฐฉ๋ฒ์ผ๋ก ๋๋๋ค.
- ์ง๋ํ์ต : ํ์ต๋ฐ์ดํฐ์ ํ๊น ๋ ์ด๋ธ ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ฑ ๋ถ์ ํ์ต์ ์ํํ ๋ค ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ค๋ฅธ ๋ฐ์ดํฐ์ ๊ฐ์ฑ์ ์์ธกํ๋ ๋ฐฉ๋ฒ
- ๋น์ง๋ ํ์ต : 'Lexicon'์ด๋ผ๋ ๊ฐ์ฑ ์ดํ ์ฌ์ ์ ์ด์ฉํด ๊ฐ์ฑ ๋ถ์์ ์ํ ์ฉ์ด์ ๋ฌธ๋งฅ์ ๋ํ ์ ๋ณด๋ฅผ ํ์ ํด ๋ฌธ์์ ๊ธ์ /๋ถ์ ์ ํ๋จ
์ค๋์ ์ง๋ํ์ต์ ์ด์ฉํ ๊ฐ์ฑ๋ถ์์ ํด๋ณด๋ ค๊ณ ํ๋ค! ๏ผ๏ฟฃ๏ธถ๏ฟฃ๏ผโใ
๋ฐ์ดํฐ์ ๋ค์ด
https://www.kaggle.com/c/word2vec-nlp-tutorial/data
Bag of Words Meets Bags of Popcorn | Kaggle
www.kaggle.com
๋จผ์ , ์บ๊ธ์์ ๋ฐ์ดํฐ ์ ์ ๋ค์ด๋ฐ์์ค๋ค.
์์ ์ค์ต
1. ๋ฐ์ดํฐ์ ๋ถ๋ฌ์ค๊ธฐ ๋ฐ ๋ฐ์ดํฐ ํ์ธ
import pandas as pd
review_df = pd.read_csv(r"C:\Users\suhye\Desktop\Kaggle\1.Popcorn\labeledTrainData.tsv\labeledTrainData.tsv",header=0,sep="\t")
review_df.head()
๋ถ๋ฌ์จ ๋ฐ์ดํฐ ์ ์ ํ์ธํด ๋ณด๋ id, sentiment(1-๊ธ์ ์ ํ๊ฐ, 2- ๋ถ์ ์ ํ๊ฐ), review๋ก ์ด๋ฃจ์ด์ ธ ์๋ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
print(review_df['review'][0])
์ฒซ๋ฒ์งธ ์ํ ๋ฆฌ๋ทฐ ํ ์คํธ๋ฅผ ํ์ธํด ๋ณธ ๊ฒฐ๊ณผ,
<br /> ํ๊ทธ๊ฐ ์กด์ฌํ๋ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
HTMLํ์์์ ์ถ์ถํ๊ธฐ ๋๋ฌธ์ด๋ค!
2. ๋ฌธ์์ด๋ก ๋ณํ
import re
#<br> html ํ๊ทธ๋ replace ํจ์๋ก ๊ณต๋ฐฑ์ผ๋ก ์ ํ
review_df['review'] = review_df['review'].str.replace('<br />', ' ')
#ํ์ด์ฌ์ ์ ๊ท ํํ์ ๋ชจ๋์ธ re๋ฅผ ์ด์ฉํด ์์ด ๋ฌธ์์ด์ด ์๋ ๋ฌธ์๋ ๋ชจ๋ ๊ณต๋ฐฑ์ผ๋ก ๋ณํ
review_df['review'] = review_df['review'].apply(lambda x : re.sub("[^a-zA-Z]"," ",x))
replaceํจ์๋ฅผ ์ด์ฉํ์ฌ html ํ๊ทธ๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๋ฐ๊ฟ์ฃผ์๋ค.
๋ํ ์์ด๊ฐ ์๋ ํน์ ๋ฌธ์ ๋ฑ์ ํผ์ฒ๋ก์จ์ ์๋ฏธ๊ฐ ์๊ธฐ ๋๋ฌธ์ ๊ณต๋ฐฑ์ผ๋ก ๋ณํธ๋ํด์ฃผ์๋ค.
[^a-zA-A]๋ ์์ด ๋, ์๋ฌธ์๊ฐ ์๋ ๋ชจ๋ ๋ฌธ์๋ฅผ ์ฐพ๋ ๊ฒ์ด๋ค.
3. train/test ๋ฐ์ดํฐ๋ก ๋ถํ
from sklearn.model_selection import train_test_split
class_df = review_df['sentiment']
feature_df = review_df.drop(['id','sentiment'],axis=1,inplace=False)
X_train,X_test,y_train,y_test = train_test_split(feature_df,class_df,test_size=0.3,random_state=156)
print("Train data shape : ",X_train.shape)
print("Test data shape : ",X_test.shape)
train_test_split ๋ชจ๋์ ์ด์ฉํ์ฌ ๋ฐ์ดํฐ ์ ์ ํ๋ จ๋ฐ์ดํฐ์ ํ ์คํธ ๋ฐ์ดํฐ๋ก ๋ถํ ํด ์ฃผ์๋ค.
Train ๋ฐ์ดํฐ์๋ 17500๊ฐ, Test ๋ฐ์ดํฐ์๋ 7500๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒ์ ํ์ธ
4. ๋ชจ๋ธ ์์ฑ(ConVectorizer)
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, roc_auc_score
#์คํฑ์๋๋ english, ngram ์ (1,2)๋ก ์ค์ ํด CountVectorization ์ํ
pipeline = Pipeline([('cnt_vect',CountVectorizer(stop_words='english',ngram_range=(1,2))),
('lr',LogisticRegression(C=10))])
#Pipeline์ ์ด์ฉํด fit,predict
pipeline.fit(X_train['review'],y_train)
pred = pipeline.predict(X_test['review'])
pred_probs = pipeline.predict_proba(X_test['review'])[:,1]
print("์์ธก ์ ํ๋ : {0:.4f} ".format(accuracy_score(y_test,pred)))
print("ROC-AUC : {0:.4f}".format(roc_auc_score(y_test,pred_probs)))
๋ฆฌ๋ทฐ ํ ์คํธ๋ฅผ ํผ์ฒ ๋ฒกํฐํํ ํ, ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ์ ์ ์ฉํ์ฌ ์์ธก ์ฑ๋ฅ์ ์ธก์ ํด ๋ณด์๋ค.
Pipeline ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ ํผ์ฒ ๋ฒกํฐํ์ ๋ก์ง์คํฑํ๊ท๋ชจ๋ธ์ ํ๋ฒ์ ์ํํ์๋ค.
์์ธก์ ํ๋์ ROC๋ ๊ฐ๊ฐ 0.89, 0.95๋ก ๋๊ฒ ์ธก์ ๋์๋ค.
TfidfVectorizer
#count์ ๋์ผํ ์กฐ๊ฑด์ผ๋ก ์คํ
pipeline = Pipeline([('tfidf_vect',TfidfVectorizer(stop_words='english',ngram_range=(1,2))),
('lr',LogisticRegression(C=10))])
pipeline.fit(X_train['review'],y_train)
pred = pipeline.predict(X_test['review'])
pred_probs = pipeline.predict_proba(X_test['review'])[:,1]
print("์์ธก ์ ํ๋ : {0:.4f} ".format(accuracy_score(y_test,pred)))
print("ROC-AUC : {0:.4f}".format(roc_auc_score(y_test,pred_probs)))
Count๋ฒกํฐ์ ๋์ผํ ์กฐ๊ฑด์ผ๋ก Tfidf๋ฒกํฐ๋ฅผ ์ด์ฉํ ๋ชจ๋ธ๋ ์์ฑํด ๋ณด์๋๋ฐ,
์์ธก ์ ํ๋์ ROC๋ชจ๋ ์กฐ๊ธ์ฉ ์ฆ๊ฐํ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
'๐ Machine Learning > ํ ์คํธ ๋ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] SentiWordNet, VADER์ ์ด์ฉํ ์ํ ๊ฐ์ํ ๊ฐ์ฑ ๋ถ์ (0) | 2022.02.21 |
---|---|
[Python] ๊ฐ์ฑ๋ถ์ - ๋น์ง๋ ํ์ต (0) | 2022.02.20 |
[Python] ๋ด์ค ๊ทธ๋ฃน ๋ถ๋ฅ (0) | 2022.02.19 |
[Python]Bag of Words(BOW) (0) | 2022.02.18 |
[Python]ํ ์คํธ ์ ์ฒ๋ฆฌ - ํ ์คํธ ์ ๊ทํ (0) | 2022.02.17 |