일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- python buildpacks
- Selenium
- 파이썬
- 알고리즘
- 크롤링
- 코랩 런타임
- geoDataFrame
- NLP
- 플라스크
- 괄호 문제
- to shp
- convert to shp
- Chat-GPT
- 혁신성장부문
- Python
- flask
- 인스타그램
- 백준
- string to list
- 웹페이지
- Merge Repositories
- clustering
- colab runtime
- 셀레니움
- kmeans
- 해시태그
- 2164 카드2
- plotly dash
- Crawling
- geopandas
Archives
- Today
- Total
코딩코딩코딩
파이썬 네이버 뉴스 일일 기사 크롤링 - 3 본문
https://hansuho113.tistory.com/4
이전 글에 이어서 클러스터별 대표 기사를 추출하는 작업을 진행할 예정
* 크롤링 날짜를 바꾸고 실행해서 클러스터링 기사 내용이 조금 다릅니다.
cluster_centers = km_cluster.cluster_centers_
print('cluster_centers shape : ', cluster_centers.shape)
print(cluster_centers)
클러스터별 중심점을 기준으로 가까이에 위치한 단어를 가져오는 작업을 위해 행렬형태로 문장을 표현
# 군집별 top n 핵심단어, 그 단어의 중심 위치 상대값, 대상 제목들을 반환함.
def get_cluster_details(cluster_model, cluster_data, feature_names, clusters_num, top_n_features=10):
cluster_details = {}
# cluster_centers array 의 값이 큰 순으로 정렬된 index 값을 반환
# 군집 중심점(centroid)별 할당된 word 피처들의 거리값이 큰 순으로 값을 구하기 위함.
centroid_feature_ordered_ind = cluster_model.cluster_centers_.argsort()[:,::-1]
#개별 군집별로 iteration하면서 핵심단어, 그 단어의 중심 위치 상대값, 대상 제목 입력
for cluster_num in range(clusters_num):
# 개별 군집별 정보를 담을 데이터 초기화.
cluster_details[cluster_num] = {}
cluster_details[cluster_num]['cluster'] = cluster_num
# cluster_centers_.argsort()[:,::-1] 로 구한 index 를 이용하여 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_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]['title']
filenames = filenames.values.tolist()
cluster_details[cluster_num]['title'] = filenames
return 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('Title :',cluster_detail['title'][:7])
print('==================================================')
feature_names = tfidf_vect.get_feature_names()
cluster_details = get_cluster_details(cluster_model=km_cluster, cluster_data=topic_df,\
feature_names=feature_names, clusters_num=clusters_num, top_n_features=10 )
print_cluster_details(cluster_details)
클러스터별 Top Feature 10개를 출력하고 대표 기사의 제목을 가져오는 함수
def WordSimilarity(word, count):
model = Word2Vec(sentences=WordVoca, size=100, window=5, min_count=5, workers=4, sg=1)
model_result = model.most_similar(positive=[word], topn=count)
Similarity_df = pd.DataFrame(model_result, columns=[word,'Similarity'])
print('{0}과 유사한 단어 Top {1} :'.format(word, count))
print(Similarity_df)
WordSimilarity('더존', 10)
위의 함수를 이용해 단어 유사도를 구할 수도 있습니다. 단 여기서 사용한 Word2Vec 은 사전에 학습시킨 단어 리스트가 있어야 합니다. 이전 글에서 작성했던 코드 중 WordVoca를 만든 내용이 있으니 참고하시기 바랍니다.
전체코드는 깃허브에서 업로드해놨습니다.
https://github.com/hansuho113/Internship/tree/master/Week2
'파이썬 > 텍스트마이닝' 카테고리의 다른 글
인스타그램 해시태그 크롤링 및 분석 - 3 (1) | 2020.07.24 |
---|---|
인스타그램 해시태그 크롤링 및 분석 - 2 (5) | 2020.07.23 |
인스타그램 해시태그 크롤링 및 분석 - 1 (1) | 2020.07.21 |
파이썬 네이버 뉴스 일일 기사 크롤링 - 2 (1) | 2020.07.20 |
파이썬 네이버 뉴스 일일 기사 크롤링 - 1 (1) | 2020.07.17 |
Comments