์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- DBSCAN
- ๋ ๋ฆฝํ๋ณธ
- opencv
- ์ธ๋์ํ๋ง
- ๋์ํ๋ณธ
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- t-test
- ADsP
- datascience
- ์๋ํด๋ผ์ฐ๋
- ์ค๋ฒ์ํ๋ง
- Lambda
- ํฌ๋กค๋ง
- ADP
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- pandas
- ๊ตฐ์งํ
- iloc
- ์ฃผ์ฑ๋ถ๋ถ์
- numpy
- dataframe
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- Python
- PCA
- LDA
- ๋น ๋ฐ์ดํฐ
- ํ ์คํธ๋ถ์
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- ํ์ด์ฌ
- ๋ฐ์ดํฐ๋ถ์
Data Science LAB
[Python] ๋ฌธ์ ๊ตฐ์งํ ๋ณธ๋ฌธ
๋ฌธ์ ๊ตฐ์งํ๋?
๋น์ทํ ํ ์คํธ ๊ตฌ์ฑ์ ๋ฌธ์๋ฅผ ๊ตฐ์งํ(Clustering)ํ๋ ๊ฒ์ด๋ค.
๋์ผํ ๊ตฐ์ง์ ์ํ๋ ๋ฌธ์๋ฅผ ๊ฐ์ ์นดํ ๊ณ ๋ฆฌ ์์์ผ๋ก ๋ถ๋ฅํ๋ ๊ฒ์ด์ง๋ง, ๋น์ง๋ํ์ต ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ค๋ ์ ์ด ํ ์คํธ ๋ถ๋ฅ์๋ ๋ค๋ฅด๋ค.
๋ฐ์ดํฐ์ ๋ค์ด
https://archive.ics.uci.edu/ml/datasets/Opinosis+Opinion+%26frasl%3B+Review
UCI Machine Learning Repository: Opinosis Opinion ⁄ Review Data Set
Opinosis Opinion ⁄ Review Data Set Download: Data Folder, Data Set Description Abstract: This dataset contains sentences extracted from user reviews on a given topic. Example topics are “performance of Toyota Camryâ€ย and “sound quality o
archive.ics.uci.edu
์์ ๋งํฌ๋ก ๋ค์ด๊ฐ ๋ค,
๋ฐ์ดํฐ ํด๋๋ฅผ ๋ค์ด๋ฐ๋๋ค.
์์ถํ์ผ์ ํ๋ฉด
๋ค์๊ณผ ๊ฐ์ ํํ์ ๋๋ ํ ๋ฆฌ๋ก ์ด๋ฃจ์ด์ ธ ์๋ค.
topics ๋๋ ํฐ๋ฆฌ ์์๋ 51๊ฐ์ ํ์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋ค.
์ด ํ์ผ๋ค์ ์ด์ฉํ์ฌ ๋ฌธ์ ๊ตฐ์งํ๋ฅผ ์งํํด ๋ณด๋ ค๊ณ ํ๋ค.
์ฃผํผํฐ ๋ ธํธ๋ถ์ผ๋ก ๋ฐ์ดํฐ์ ๋ถ๋ฌ์ค๊ธฐ
import pandas as pd
import glob, os
path = r'C:\Users\OpinosisDataset1.0\OpinosisDataset1.0\topics'
#path๋ก ์ง์ ํ ๋๋ ํ ๋ฆฌ ๋ฐ์ ๋ชจ๋ .data ํ์ผ์ ํ์ผ๋ช
์ ๋ฆฌ์คํธ๋ก ์ทจํฉ
all_files = glob.glob(os.path.join(path,"*.data"))
filename_list = []
opinion_text = []
for file_ in all_files:
df = pd.read_table(file_,index_col = None, header=0, encoding='latin1')
#์ ๋ ๊ฒฝ๋ก๋ก ์ฃผ์ด์ง ํ์ผ๋ช
์ ๊ฐ๊ณต, ๋ฆฌ๋
์ค์์ ์ํํ ๋๋ ๋ค์ \\์ /๋ก ๋ณ๊ฒฝ
#๋งจ ๋ง์ง๋ง .dataํ์ฅ์ ์ ๊ฑฐ
filename_ = file_.split('\\')[-1]
filename = filename_.split('.')[0]
#ํ์ผ๋ช
๋ฆฌ์คํธ์ ํ์ผ ๋ด์ฉ ๋ฆฌ์คํธ์ ํ์ผ๋ช
๊ณผ ํ์ผ ๋ด์ฉ ์ถ๊ฐ
filename_list.append(filename)
opinion_text.append(df.to_string())
#ํ์ผ๋ช
list์ ํ์ผ ๋ด์ฉ list ๊ฐ์ฒด๋ฅผ DataFrame์ผ๋ก ์์ฑ
document_df = pd.DataFrame({'filename':filename_list,
'opinion_text': opinion_text})
document_df.head()
์ฌ์ฉ์์ PC ๊ฒฝ๋ก์ ๋ง์ถฐ ๋ฐ์ดํฐ ์ ์ ๋ถ๋ฌ์จ ํ, path๋ก ์ง์ ํ ๋๋ ํ ๋ฆฌ ๋ฐ์ ๋ชจ๋ .data ํ์ผ์ ํ์ผ๋ช ์ ๋ฆฌ์คํธ๋ก ์ทจํฉํ๋ค.
๊ฐ๋ณ ํ์ผ์ ํ์ผ๋ช ์ filenam_list๋ก ์ทจํฉํ๊ณ , ํ์ผ์ ๋ด์ฉ์ ๋ก๋ฉ ํ ๋ค์ string์ผ๋ก ๋ณํํ์ฌ opinion_text list๋ก ์ทจํฉํ๋ค.
DataFrame์ ์์ฑํ๋ For ๋ฌธ์ ์์ฑํ์ฌ ๋๋ ํ ๋ฆฌ ๋ฐ์ ๋ชจ๋ ํ์ผ์ ํ๋์ฉ ์ถ๊ฐํ์ฌ list๋ก ์์ฑํ๊ณ ํ์ผ๋ช ์ ๊ฐ๊ณตํ๊ณ ํ์ฅ์๋ฅผ ์ ๊ฑฐํ์ฌ ์ต์ข ์ ์ธ DataFrame์ ์์ฑํ๋ค.
TF-IDF ํํ๋ก ํผ์ฒ ๋ฒกํฐํ
from nltk.stem import WordNetLemmatizer
import nltk
import string
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
lemmar = WordNetLemmatizer()
def LemTokens(tokens):
return [lemmar.lemmatize(token) for token in tokens]
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
Lemmatization์ ๊ตฌํํ๋ LemNormalize(text)ํจ์๋ฅผ ์์ฑํด ์ฃผ์๋ค.
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vect = TfidfVectorizer(tokenizer = LemNormalize, stop_words = 'english',
ngram_range = (1,2),min_df=0.05,max_df = 0.85)
#opinion_text ์นผ๋ผ ๊ฐ์ผ๋ก ํผ์ฒ ๋ฒกํฐํ ์ํ
feature_vect = tfidf_vect.fit_transform(document_df['opinion_text'])
๊ฐ๋ณ ๋ฌธ์ ํ ์คํธ์ ๋ํด TF-IDF ๋ณํ๋ ํผ์ฒ ๋ฒกํฐํ๋ ํ๋ ฌ์ ๊ตฌํ ์ ์์
K-Means Clustering (n = 5) ์ํ
from sklearn.cluster import KMeans
km = KMeans(n_clusters = 5, max_iter = 10000, random_state=0)
km.fit(feature_vect)
cluster_label = km.labels_
cluster_centers = km.cluster_centers_
document_df['cluster_label'] = cluster_label
document_df.head()
์ค์ฌ(n_cluster)๋ฅผ 5๊ฐ๋ก ์ค์ ํ ๋ค, KMeans ๋ชจ๋ธ์ ์์ฑํ์์ผ๋ฉฐ
cluster_label ์ปฌ๋ผ์ ์ถ๊ฐํ์ฌ ๊ฐ ๋ฌธ์๊ฐ ์ด๋ค ๊ตฐ์ง์ผ๋ก ๊ตฐ์งํ๋์๋์ง ํ์ธํ ์ ์๋๋ก ํ์๋ค.
#๋ฐ์ดํฐ์ ๋ ฌ
document_df[document_df['cluster_label'] == 0].sort_values(by='filename')
cluster_label์ด 0์ธ ๋ฐ์ดํฐ์ ๋ง ์กฐํํ ๊ฒฐ๊ณผ,
0๋ฒ ๊ตฐ์ง์ ํธํ ๊ณผ ๊ด๋ จ๋ ๋ฌธ์๋ค๋ก ๊ตฐ์งํ ๋์ด์ ธ ์์์ ์ ์ ์์๋ค.
document_df[document_df['cluster_label'] == 1].sort_values(by='filename')
๋ง์ฐฌ๊ฐ์ง๋ก, ๊ตฐ์ง1์ ํจ๋ค, ์์ดํ ๋ฑ์ ์ ์๊ธฐ๊ธฐ์ ๋ํ ๋ฆฌ๋ทฐ๋ค๋ก ๊ตฐ์งํ๋์ด์ ธ ์์์ ์์ ์์๋ค.
'
K-Means Clustering(n=3) ์ํ
from sklearn.cluster import KMeans
km = KMeans(n_clusters = 3, max_iter = 10000, random_state=0)
km.fit(feature_vect)
cluster_label = km.labels_
cluster_centers = km.cluster_centers_
document_df['cluster_label'] = cluster_label
document_df.sort_values(by = 'cluster_label')
์์ ๋ชจ๋ธ๊ณผ ๋์ผํ๊ฒ KMeans ๋ชจ๋ธ์ด์ง๋ง,
n_cluster๊ฐ์ 3์ผ๋ก ์กฐ์ ํ์ฌ ๋ชจ๋ธ์ ๋ค์ ์์ฑํ์๋ค.
๊ตฐ์ง ๋ณ ํต์ฌ๋จ์ด ์ถ์ถ
cluster_centers = km.cluster_centers_
print('cluster_centers shape : ',cluster_centers.shape)
print(cluster_centers)
cluster_centers๋ (3,4611)๋ฐฐ์ด๋ก ์ด๋ฃจ์ด์ ธ ์์ผ๋ฉฐ,
3๊ฐ์ ๊ตฐ์ง, 4611๊ฐ์ ํผ์ฒ๋ก ๊ตฌ์ฑ๋์ด์์์ ์๋ฏธํ๋ค.
๊ฐ ๋ฐฐ์ด ๊ฐ์ 0~1์ฌ์ด์ ๊ฐ์ผ๋ก ํผ์ฒ๊ฐ ์ค์ฌ๊ฐ๊ณผ ์ผ๋ง๋ ๊ฐ๊น์ด ์์นํ๊ณ ์๋ ์ง๋ฅผ ์๋ฏธํ๋ค.
#๊ตฐ์ง๋ณ top n ํต์ฌ ๋จ์ด, ๊ทธ ๋จ์ด์ ์ค์ฌ ์์น ์๋๊ฐ, ๋์ ํ์ผ๋ช
๋ฐํ
def get_cluster_details(cluster_model,cluster_data,feature_names,clusters_num,top_n_features=10):
cluster_details = {}
#cluster_centers array์ ๊ฐ์ด ํฐ ์์ผ๋ก ์ ๋ ฌ๋ ์ธ๋ฑ์ค ๊ฐ ๋ฐํ
#๊ตฐ์ง ์ค์ฌ์ ๋ณ ํ ๋น๋ word ํผ์ฒ๋ค์ ๊ฑฐ๋ฆฌ๊ฐ์ด ํฐ ์์ผ๋ก ๊ฐ์ ๊ตฌํ๊ธฐ ์ํจ
centroid_feature_ordered_ind = cluster_model.cluster_centers_.argsort()[:,::-1]
#๊ฐ๋ณ ๊ตฐ์ง๋ณ๋ก ๋ฐ๋ณตํ๋ฉด์ ํต์ฌ ๋จ์ด, ๊ทธ ๋จ์ด์ ์ค์ฌ ์์น ์๋๊ฐ, ๋์ ํ์ผ๋ช
์
๋ ฅ
for cluster_num in range(clusters_num):
cluster_details[cluster_num] = {}
cluster_details[cluster_num]['cluster'] = cluster_num
#top n ํผ์ฒ ๋จ์ด ๊ตฌํ๊ธฐ
top_feature_indexes = centroid_feature_ordered_ind[cluster_num,:top_n_features]
top_features = [ feature_names[ind] for ind in top_feature_indexes ]
#ํด๋น ํผ์ฒ ๋จ์ด์ ์ค์ฌ ์์น ์๋๊ฐ ๊ตฌํ๊ธฐ
top_feature_values = cluster_model.cluster_centers_[cluster_num,top_feature_indexes].tolist()
#cluster_details ๋์
๋๋ฆฌ ๊ฐ์ฒด์ ๊ฐ๋ณ ๊ตฐ์ง๋ณ ํต์ฌ๋จ์ด์ ์ค์ฌ์์น ์๋๊ฐ, ํด๋น ํ์ผ๋ช
์
๋ ฅ
cluster_details[cluster_num]['top_features'] = top_features
cluster_details[cluster_num]['top_features_value'] = top_feature_values
filenames = cluster_data[cluster_data['cluster_label'] == cluster_num]['filename']
filenames = filenames.values.tolist()
cluster_details[cluster_num]['filenames'] = filenames
return cluster_details
get_cluster_details()๋ฅผ ํธ์ถํ๋ฉด dictionary๋ฅผ ์์๋ก ๊ฐ์ง๋ ๋ฆฌ์คํธ์ธ cluster_details๋ฅผ ๋ฐํํ๋ค.
cluster_details์๋ ๊ฐ๋ณ ๊ตฐ์ง ๋ฒํธ, ํต์ฌ ๋จ์ด, ํต์ฌ ๋จ์ด ์ค์ฌ ์์น ์๋๊ฐ, ํ์ผ๋ช ์์ฑ ๊ฐ ์ ๋ณด๊ฐ ์๋ค.
def print_cluster_details(cluster_details):
for cluster_num, cluster_detail in cluster_details.items():
print('-----Cluster {0}'.format(cluster_num))
print('Top features: ',cluster_detail['top_features'])
print('Reviews ํ์ผ๋ช
: ',cluster_detail['filenames'][:7])
print("=======================================================")
cluster_details๋ฅผ ์์ธํ ๋ณด๊ธฐ ์ํ ํจ์๋ฅผ ๋ฐ๋ก ์์ฑํ์๋ค.
feature_names = tfidf_vect.get_feature_names()
cluster_details = get_cluster_details(cluster_model = km, cluster_data = document_df,
feature_names = feature_names, clusters_num=3, top_n_features=10)
print_cluster_details(cluster_details)
์์ฑํ ํจ์๋ค์ ํธ์ถํ ๊ฒฐ๊ณผ
get_cluster_detail() ํจ์๋ฅผ ํธ์ถํ๋ฉด KMeans ๊ตฐ์งํ ๊ฐ์ฒด, ํ์ผ๋ช ์ถ์ถ์ ์ํ document_df DataFrame, ํต์ฌ ๋จ์ด ์ถ์ถ์ ์ํ ํผ์ฒ๋ช ๋ฆฌ์คํธ, ์ ์ฒด ๊ตฐ์ง ๊ฐ์, ํต์ฌ ๋จ์ด ์ถ์ถ ๊ฐ์๋ฅผ ๋ฐํํ๋ค.
'๐ Machine Learning > ํ ์คํธ ๋ถ์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] ํ๊ธ ํ ์คํธ ์ฒ๋ฆฌ - ๋ค์ด๋ฒ ์ํ ํ์ ๊ฐ์ฑ ๋ถ์ (0) | 2022.02.26 |
---|---|
[Python] ๋ฌธ์ ์ ์ฌ๋ (0) | 2022.02.25 |
[Python] ํ ํฝ ๋ชจ๋ธ๋ง (20 ๋ด์ค๊ทธ๋ฃน) (0) | 2022.02.22 |
[Python] SentiWordNet, VADER์ ์ด์ฉํ ์ํ ๊ฐ์ํ ๊ฐ์ฑ ๋ถ์ (0) | 2022.02.21 |
[Python] ๊ฐ์ฑ๋ถ์ - ๋น์ง๋ ํ์ต (0) | 2022.02.20 |