์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ๋น ๋ฐ์ดํฐ
- datascience
- ๋์ํ๋ณธ
- ๋ฐ์ดํฐ๋ถ์
- ADsP
- LDA
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- ์ธ๋์ํ๋ง
- ๋ ๋ฆฝํ๋ณธ
- pandas
- Python
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- dataframe
- DBSCAN
- Lambda
- ์ฃผ์ฑ๋ถ๋ถ์
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- numpy
- PCA
- t-test
- ์ค๋ฒ์ํ๋ง
- ์๋ํด๋ผ์ฐ๋
- opencv
- ํ์ด์ฌ
- ํ ์คํธ๋ถ์
- ๊ตฐ์งํ
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- ํฌ๋กค๋ง
- iloc
- ADP
Data Science LAB
[Python] ํ ํฝ ๋ชจ๋ธ๋ง (20 ๋ด์ค๊ทธ๋ฃน) ๋ณธ๋ฌธ
[Python] ํ ํฝ ๋ชจ๋ธ๋ง (20 ๋ด์ค๊ทธ๋ฃน)
ใ ใ ใ ใ 2022. 2. 22. 17:15Topic Modeling
ํ ํฝ ๋ชจ๋ธ๋ง์ด๋ ๋ฌธ์ ์งํฉ์ ์จ์ด ์๋ ์ฃผ์ ๋ฅผ ์ฐพ์๋ด๋ ๊ฒ์ด๋ค. ๋จธ์ ๋ฌ๋ ๊ธฐ๋ฐ์ ํ ํฝ ๋ชจ๋ธ์ ์จ๊ฒจ์ง ์ฃผ์ ๋ฅผ ํจ๊ณผ์ ์ผ๋ก ํํํ ์ ์๋ ์ค์ฌ ๋จ์ด๋ฅผ ํจ์ถ์ ์ผ๋ก ์ถ์ถํด๋ธ๋ค.
ํ ํฝ๋ชจ๋ธ๋ง์์๋ LDA(Latent Dirichlet Allocation)์ ์ฃผ๋ก ํ์ฉํ๋ค. ํํ ๋จธ์ ๋ฌ๋์์ ์ฌ์ฉํ๋ LDA(Linear Discriminant Analysis)์๋ ๋ค๋ฅธ ์๊ณ ๋ฆฌ์ฆ์ด๋ฏ๋ก ์ฃผ์ํด์ผํ๋ค.
๊ธฐ๋ณธ ๋ฐ์ดํฐ์ ์ธ 20๋ด์ค๊ทธ๋ฃน ๋ฐ์ดํฐ ์ ์ ์ด์ฉํ์ฌ ํ ํฝ๋ชจ๋ธ๋ง์ ์งํํด๋ณด๋ ค๊ณ ํ๋ค.
20๋ด์ค๊ทธ๋ฃน ๋ฐ์ดํฐ์ ์๋ 20๊ฐ์ง์ ์ฃผ์ ๋ฅผ ๊ฐ์ง ๋ด์ค๊ทธ๋ฃน์ ๋ฐ์ดํฐ๊ฐ ์๋๋ฐ, ๊ทธ ์ค 8๊ฐ์ ์ฃผ์ ๋ฅผ ์ถ์ถํ๊ณ , ์ด๋ค ํ ์คํธ์ LDA ๊ธฐ๋ฐ์ ํ ํฝ ๋ชจ๋ธ๋ง์ ์ ์ฉํด๋ณด๋ ค๊ณ ํ๋ค.
ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ก๋ฉ ํ ์นดํ ๊ณ ๋ฆฌ ์ถ์ถ
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
#๋งฅ, ์๋์ฐ์ฆ, ์ผ๊ตฌ, ํํค, ์ค๋, ๊ธฐ๋
๊ต, ์ ์๊ณตํ, ์ํ 8๊ฐ ์ฃผ์ ์ถ์ถ
cats = ['comp.sys.mac.hardware','comp.windows.x','rec.sport.baseball','rec.sport.hockey','talk.politics.mideast','soc.religion.christian','sci.electronics','sci.med']
#cats ๋ณ์๋ก ๊ธฐ์ฌ๋ ์นดํ
๊ณ ๋ฆฌ๋ง ์ถ์ถ
news_df = fetch_20newsgroups(subset='all',remove=('headers','footers','quotos'),categories = cats,random_state=0)
#Count๊ธฐ๋ฐ์ ๋ฒกํฐํ๋ง ์ ์ฉ
count_vect = CountVectorizer(max_df=0.95,max_features=1000,min_df=2,stop_words='english',ngram_range=(1,2))
feat_vect = count_vect.fit_transform(news_df.data)
print('CountVectorizer Shape : ',feat_vect.shape)
๋งฅ, ์๋์ฐ์ฆ, ์ผ๊ตฌ, ํํค, ์ค๋, ๊ธฐ๋ ๊ต, ์ ์๊ณตํ, ์ํ ์ด 8๊ฐ์ ์ฃผ์ ๋ฅผ ์ถ์ถํ ํ
์ถ์ถ๋ ํ ์คํธ๋ฅผ Count๊ธฐ๋ฐ์ผ๋ก ๋ฒกํฐํ ๋ณํํ์๋ค.
(LDA๋ Count๊ธฐ๋ฐ์ ๋ฒกํฐํ๋ง ์ ์ฉ ๊ฐ๋ฅํจ)
CounterVectorizer ๊ฐ์ฒด ๋ณ์์ธ feat_vect๋ 7855๊ฐ์ ๋ฌธ์๊ฐ 1000๊ฐ์ ํผ์ฒ๋ก ๊ตฌ์ฑ๋ ํ๋ ฌ๋ฐ์ดํฐ์ด๋ค.
์ด๋ ๊ฒ ํผ์ฒ ๋ฒกํฐํ๋ ๋ฐ์ดํฐ์ ์ ๊ธฐ๋ฐ์ผ๋ก LDA ํ ํฝ ๋ชจ๋ธ๋ง์ ์ํํ๋ ค๊ณ ํ๋ค.
lda = LatentDirichletAllocation(n_components=8,random_state=0)
lda.fit(feat_vect)
print(lda.components_.shape)
lda.components_
LatentDirichletAllocation์ n_components๋ ์์์ ์ถ์ถํ ์ฃผ์ ์ ๊ฐ์์ ๋์ผํ 8๊ฐ๋ก ์ค์ ํ์๋ค.
components๋ ๊ฐ ํ ํฝ๋ณ๋ก words ํผ์ฒ๊ฐ ์ผ๋ง๋ ๋ง์ด ๊ทธ ํ ํฝ์ ํ ๋น ๋๋์ง ์์น๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
(๋์ ๊ฐ์ผ ์๋ก ํด๋น word ํผ์ฒ๋ ์ค์ฌ word)
8๊ฐ์ ํ ํฝ๋ณ๋ก 1000๊ฐ์ word ํผ์ฒ๊ฐ ํด๋น ํ ํฝ๋ณ๋ก ์ฐ๊ด๋ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ค.
ํจ์ ์์ฑ
def display_topics(model,feature_names,no_top_words):
for topic_index,topic in enumerate(model.components_):
print('Topic #',topic_index)
#components_array์์ ๊ฐ์ฅ ๊ฐ์ด ํฐ ์์ผ๋ก ์ ๋ ฌํ์ ๋, ๊ทธ ๊ฐ์ array ์ธ๋ฑ์ค ๋ฐํ
topic_word_indexes = topic.argsort()[::-1]
top_indexes = topic_word_indexes[:no_top_words]
#top_indexes ๋์์ธ ์ธ๋ฑ์ค ๋ณ๋ก feature_names์ ํด๋นํ๋ word feature ์ถ์ถ ํ join์ผ๋ก concat
feature_concat = ' '.join([feature_names[i] for i in top_indexes])
print(feature_concat)
#CountVectorizer ๊ฐ์ฒด ๋ด์ ์ ์ฒด word์ ๋ช
์นญ์ get_features_names()๋ฅผ ํตํด ์ถ์ถ
feature_names = count_vect.get_feature_names()
#ํ ํฝ๋ณ๋ก ๊ฐ์ฅ ์ฐ๊ด๋ ๋์ word 15๊ฐ์ฉ ์ถ์ถ
display_topics(lda,feature_names,15)
display_topics()ํจ์๋ฅผ ์์ฑํ์ฌ ๊ฐ ํ ํฝ๋ณ๋ก ์ฐ๊ด๋๊ฐ ๋์ ์์๋๋ก words๋ฅผ ์ถ๋ ฅํ์๋ค.
'๐ Machine Learning > ํ ์คํธ ๋ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] ๋ฌธ์ ์ ์ฌ๋ (0) | 2022.02.25 |
---|---|
[Python] ๋ฌธ์ ๊ตฐ์งํ (0) | 2022.02.24 |
[Python] SentiWordNet, VADER์ ์ด์ฉํ ์ํ ๊ฐ์ํ ๊ฐ์ฑ ๋ถ์ (0) | 2022.02.21 |
[Python] ๊ฐ์ฑ๋ถ์ - ๋น์ง๋ ํ์ต (0) | 2022.02.20 |
[Python] ๊ฐ์ฑ ๋ถ์(Sentiment Analysis) - ์ง๋ํ์ต (0) | 2022.02.19 |