[토픽모델링] 토픽트렌드 구하기(수정)

Author : tmlab / Date : 2016. 10. 18. 17:40 / Category : Text Mining/R

토픽 모델링

  • 사용한 패키지
    • excel file I/O
      • readxl: excel file input
      • openxlsx: excel file output
    • string 처리 및 text mining 패키지
      • tm: R 제공 text mining 패키지, corpus, tdm 및 dtm 등을 제공
      • stringr: string 처리 패키지
    • topic modeling 패키지
      • topicmodels, lda: lda 패키지, 본인이 배운걸로는 topicmodels로 lda 포맷 변환후 lda로 돌리는게 정확도가 높다고 들음
In [1]:
library(readxl)
  • 앞서 형태소 분석한 파일을 로딩
In [2]:
blog<- read_excel("after_POS.xlsx")
  • 데이터 살펴보기
In [3]:
str(blog)
Classes 'tbl_df', 'tbl' and 'data.frame':	206 obs. of  5 variables:
 $            : chr  "1" "2" "3" "4" ...
 $ a*****     : chr  "ㅁ**" "ㅋ**" "녹***" "맥****" ...
 $ date       : chr  "2015.04.18 05:18" "2015.04.18 07:26" "2015.04.18 10:55" "2015.04.18 11:10" ...
 $ desc       : chr  "첫부분만 읽고서는 그저 그러뉴변명하려는 줄 알고 짜증부터 났네요. 방청품질이 미국과 동일하다고 두괄식으로 결론부터 쓰시는게 좋을"| __truncated__ "이제까지 내수 수출 차별한건 사실이고 방청말고 내부사양에 다른 장난질을 쳤을 가능성이 있다는 것도 인정 하신다는 말씀이죠?  참 대"| __truncated__ "          이분 최소한 난독증...진실한 소통의 시작..응원합니다        " "          맞는 부분 있습니다.. 내수외 북미용이 다릅니다. 씨트프레임도 달라요..ㅋㅋ 제조해 봐서... ㅋㅋ        " ...
 $ article_POS: chr  "첫/MDT;부분/NNG;만/JX;읽/VV;고서/ECD;는/JX;그저/MAG;그/VV;러/ECD;뉴/NNG;변명/NNG;하/XSV;려는/ETD;줄/NNB;알/VV;고/ECE;짜증/NNG;"| __truncated__ "이제/NNG;까지/JX;내수/NNG;수출/NNG;차별/NNG;하/XSV;ㄴ/ETD;것/NNB;은/JKS;사실/NNG;이/VCP;고/ECE;방청/NNG;말/VV;고/ECE;내부/NNG;"| __truncated__ "이분/NNG;최소/NNG;하/XSV;ㄴ/ETD;난독/NNG;증/NNG;.../SE;진실/NNG;하/XSV;ㄴ/ETD;소통/NNG;의/JKG;시작/NNG;../SW;응원/NNG;하/XSV;ㅂ"| __truncated__ "맞/VV;는/ETD;부분/NNG;있/VV;습니다/EFN;../SW;내수/NNG;외/NNB;북미/NNG;용/XSN;이/JKS;다르/VV;ㅂ니다/EFN;./SF;씨트/UN;프레임/NNG;"| __truncated__ ...
  • 글이 없어 형태소 분석칸이 공란인 데이터를 삭제
In [4]:
blog_test<-blog[complete.cases(blog$article_POS),]
In [5]:
library(tm)
library(stringr)
library(rJava)
Loading required package: NLP
In [6]:
ko.words = function(doc){
  d = str_split(doc, ';')[[1]] ## 띄어쓰기(' ')를 기준으로 한 문장을 여러 단어로 나눔 
  
  extracted = tolower(str_match(d, '([가-힣a-zA-Z]+)/[NVO]'))
  extracted2 = tolower(str_match(d,'([가-힣]+)/XR'))
  keyword = extracted[,2]
  keyword2 = extracted2[,2]
  keyword<-c(keyword,keyword2)
  
  keyword[!is.na(keyword)]  
}
  • 형태소 분석이 된 데이터를 corpus로 만든다음, tdm 작성
  • 이후 dtm으로 전환, 애시당초 만들 때 dtm으로 만들어도 무방
In [7]:
options(mc.cores=1)

cps = Corpus(VectorSource(blog_test$article_POS))

tdm <- TermDocumentMatrix(cps, control=list(tokenize=ko.words,
                                            wordLengths=c(2, Inf)))

dtm <- as.DocumentTermMatrix(tdm)
In [8]:
head(as.matrix(dtm))
aidapparcarticlearticleidasaspxautobarblog...훌륭휘어지흉기흐르흐리흔들리흔하흘리흠집힘들
10000000000...0000000000
20000000000...0000000000
30000000000...0000000000
40000000000...0000000000
50000000000...0000000000
60000010000...0000000002
  • dtm을 ldaformat으로 변환
  • lda 분석 실행
    • K는 추출할 토픽 개수
    • iterations는 사후확률의 업데이트 횟수
    • burnin은 확률 추정시 제외되는 초반부 이터레이션 값
    • alpha는 문서내에서 토픽들의 확률분포(1을 주면 유니폼 분포)
    • eta: 한 토픽 내에 단어들의 확률분포
  • start와 end는 소요시간을 체크 하기 위한 구문으로 삭제해도 무방
In [9]:
set.seed(227)

library(topicmodels)
ldaform<-dtm2ldaformat(dtm, omit_empty = F)

library(lda)

start = Sys.time()
result.lda<-lda.collapsed.gibbs.sampler(ldaform$documents,
                                        K = 10,
                                        vocab = ldaform$vocab,
                                        num.iterations = 80000,
                                        burnin = 25000,
                                        alpha = 0.01,
                                        eta = 0.01)

end <- Sys.time()

end-start
Time difference of 1.65214 mins
  • 해당 토픽에 출현한 단어 보기
    • 본 포스팅에선 20개를 살펴봄
In [10]:
top.topic.words(result.lda$topics,20,by.score=T)
아니시스한국차량현대소나타소통부식수출지도
대한민국그랜저미국우리나라지금떨어지이것기차내수부식
내수xg안전핑계부식바꾸우리방청강판지역
수출사원아니as자동차정비소산타페품질차량현지
기업어쩌sm발생만들고장진실코팅동일무관
나라영업만들소비자국민이상달리이제국내자료
부식그때외제고치생각사고오해사람차이미국
처리아부지에어백하체모르주변com시작차별국내
고객이번사실정도현지경험http하부다르자체
그러세대원가설명대하한번대하구입비교세상
워드불친절차이결함그렇bmw재질언더방청학회
표면운행절감따르아니운전미국올리들어가어이
현대지점현대사람때문뒷바퀴흉기차량일본이해
호구괜찮정도거짓말회사ef기사문제이상나라
자동차바뀌홍보기간현대자동차거기naver가지달르내리
이것hg신뢰가죽기아고무nf회사아연도금소비자
양쪽품질가격그리고객잔고결정소비자모델강판
다르거리현기시트말하잡소리말장난보시대우기준
그렇뒷자리서비스어디보이브레이크부탁버리직원그러
교체무상북미녹이위하문제없이야기그러구형부분
  • 제대로 된 전처리를 하지 않아 http나 naver가 추출 (URL임)
    • 이 후, 단어들을 보고 토픽을 주관적으로 이름 지어야 함
    • 예를 들어 topic 4를 보면, 내외수 차별에 관련된 토픽임
In [11]:
top_topic<-top.topic.words(result.lda$topics,20,by.score = T)

theta = rowSums(result.lda$document_sums)

## 비율로 변환 및 추가
topic.proportion = theta/sum(theta)

####토픽내에 단어별  출현확률(비율)구하기(여기서 상위개수 지정한거에 따라서 함수식 변환해주기)
top.words = top.topic.words(result.lda$topics, 20)#상위 n개 지정
c_top.words<-as.character(top.words)#캐릭터로 바꾸기
new.topics<-subset(result.lda$topics,select=c_top.words)#top.words에 해당하는 토픽 뽑기

count_by_words<-new.topics

a=1
k=0
for(j in 1:ncol(new.topics))#count 모아주는 함수
{
  if(a*20+1==j)
  {
    k=20*a
    a=a+1
  }
  count_by_words[a,j-k]<-count_by_words[a,j]
}


proportion_by_words<-t(count_by_words[,1:20]/as.integer(result.lda$topic_sums))#비율구하기

result<-matrix(paste(top.topic.words(result.lda$topics, 20),"(",proportion_by_words,")"),byrow=F,nrow=20)#단어랑 비율 paste해서 행렬만들기
output<-rbind(result,topic.proportion,t(result.lda$topic_sums))

토픽 트렌드 구하기

  • 앞서 행한 토픽모델링에서 각 토픽에 속한 단어의 문서별 빈도 살펴보기
In [12]:
trends<-as.data.frame(t(result.lda$document_sums),stringsAsFactors=F)
trends
V1V2V3V4V5V6V7V8V9V10
10150000320
20030010450
30000006100
40000000420
500100031800
62401106002900
7001802300000
80020000000
9011009001300
100030406000
11000201124170130
1200006000110
130300290423320
14200336420230180
151000000020
160000010000
1700000000180
1800002106000
190000570010
200901300000
210405100000
22002018001210
230090000000
241000000300
2500014000933
2679000000000
270000300100
280060000000
290100000000
3000000000063
.................................
1770004000000
1780000000500
179009161000000
18000012500000
1810001000000
1820000000000
1830000600200
1840000900000
1850200001000
1860060000006
1870000003000
188000100000100
189501800002400
19004041401000
1910001000000
1920000005000
193000016016050
19400700170000
1950030000104
19600041700110
19725000070000
1980000002000
19900011600000
2000002500000
20100012300000
2020906700400
2030000600200
2040000006200
20500001100000
2060010000000
  • 토픽트렌드를 살펴보기 위해선 시간의 흐름이 있어야 함
    • 또한 기준을 일 단위로 할건지, 월 단위로 할건지, 년 단위로 할건지 또한 미리 고려해야 함
    • 본 포스팅에서는 월 단위로 살펴볼 예정임
In [13]:
blog$date
  1. "2015.04.18 05:18"
  2.  
  3. "2015.04.18 07:26"
  4.  
  5. "2015.04.18 10:55"
  6.  
  7. "2015.04.18 11:10"
  8. "2015.04.18 08:32"
  9.  
  10. "2015.04.18 09:38"
  11.  
  12. "2015.04.18 10:18"
  13.  
  14. "2015.04.18 10:06"
  15. "2015.04.18 11:02"
  16.  
  17. "2015.04.18 11:15"
  18.  
  19. "2015.04.18 12:48"
  20.  
  21. "2015.04.18 13:05"
  22. "2015.04.18 16:42"
  23.  
  24. "2015.04.18 18:42"
  25.  
  26. "2015.04.18 19:20"
  27.  
  28. "2015.04.18 21:16"
  29. "2015.04.19 01:26"
  30.  
  31. "2015.04.19 01:51"
  32.  
  33. "2015.04.19 07:00"
  34.  
  35. "2015.04.19 11:06"
  36. "2015.04.20 17:27"
  37.  
  38. "2015.04.19 17:51"
  39.  
  40. "2015.04.20 16:57"
  41.  
  42. "2015.04.20 17:03"
  43. "2015.04.20 17:24"
  44.  
  45. "2015.04.20 17:37"
  46.  
  47. "2015.04.20 19:18"
  48.  
  49. "2015.04.20 19:20"
  50. "2015.04.20 19:30"
  51.  
  52. "2015.04.20 20:41"
  53.  
  54. "2015.04.20 21:49"
  55.  
  56. "2015.04.20 22:07"
  57. "2015.04.20 22:21"
  58.  
  59. "2015.04.20 22:57"
  60.  
  61. "2015.04.20 22:59"
  62.  
  63. "2015.04.21 02:27"
  64. "2015.04.21 11:45"
  65.  
  66. "2015.04.21 15:47"
  67.  
  68. "2015.04.21 20:31"
  69.  
  70. "2015.04.21 20:38"
  71. "2015.04.21 21:09"
  72.  
  73. "2015.04.21 21:46"
  74.  
  75. "2015.04.21 23:52"
  76.  
  77. "2015.04.21 23:52"
  78. "2015.04.22 02:00"
  79.  
  80. "2015.04.22 12:19"
  81.  
  82. "2015.04.22 16:26"
  83.  
  84. "2015.04.22 09:01"
  85. "2015.04.22 12:17"
  86.  
  87. "2015.04.22 10:19"
  88.  
  89. "2015.04.22 12:10"
  90.  
  91. "2015.04.22 12:14"
  92. "2015.04.22 12:23"
  93.  
  94. "2015.04.22 12:23"
  95.  
  96. "2015.04.22 15:20"
  97.  
  98. "2015.04.22 16:24"
  99. "2015.04.22 16:29"
  100.  
  101. "2015.04.22 16:36"
  102.  
  103. "2015.04.23 03:24"
  104.  
  105. "2015.04.23 05:01"
  106. "2015.04.23 13:42"
  107.  
  108. "2015.04.23 17:16"
  109.  
  110. "2015.04.23 18:20"
  111.  
  112. "2015.04.24 03:07"
  113. "2015.04.24 12:28"
  114.  
  115. "2015.04.24 16:56"
  116.  
  117. "2015.04.24 16:56"
  118.  
  119. "2015.04.26 01:44"
  120. "2015.04.26 01:44"
  121.  
  122. "2015.04.26 07:13"
  123.  
  124. "2015.04.26 07:17"
  125.  
  126. "2015.04.26 09:23"
  127. "2015.04.27 13:44"
  128.  
  129. "2015.04.27 14:27"
  130.  
  131. "2015.04.28 07:59"
  132.  
  133. "2015.04.28 20:56"
  134. "2015.04.28 21:21"
  135.  
  136. "2015.05.02 12:48"
  137.  
  138. "2015.05.03 21:44"
  139.  
  140. "2015.05.03 21:44"
  141. "2015.05.04 00:24"
  142.  
  143. "2015.05.04 12:52"
  144.  
  145. "2015.05.04 13:10"
  146.  
  147. "2015.05.04 16:07"
  148. "2015.05.04 17:21"
  149.  
  150. "2015.05.04 19:27"
  151.  
  152. "2015.05.04 22:46"
  153.  
  154. "2015.05.05 13:01"
  155. "2015.05.05 16:55"
  156.  
  157. "2015.05.06 08:45"
  158.  
  159. "2015.05.06 20:52"
  160.  
  161. "2015.05.13 11:36"
  162. "2015.05.17 13:10"
  163.  
  164. "2015.05.17 23:12"
  165.  
  166. "2015.05.17 23:15"
  167.  
  168. "2015.05.18 13:11"
  169. "2015.05.21 11:15"
  170.  
  171. "2015.05.21 21:27"
  172.  
  173. "2015.05.23 20:21"
  174.  
  175. "2015.05.26 15:32"
  176. "2015.05.26 15:39"
  177.  
  178. "2015.05.26 18:11"
  179.  
  180. "2015.05.26 18:39"
  181.  
  182. "2015.05.26 21:45"
  183. "2015.05.26 21:59"
  184.  
  185. "2015.05.26 22:01"
  186.  
  187. "2015.05.26 22:05"
  188.  
  189. "2015.05.27 02:33"
  190. "2015.05.27 02:43"
  191.  
  192. "2015.05.27 02:50"
  193.  
  194. "2015.05.27 05:38"
  195.  
  196. "2015.05.27 08:51"
  197. "2015.05.27 08:56"
  198.  
  199. "2015.05.27 09:03"
  200.  
  201. "2015.05.27 09:32"
  202.  
  203. "2015.05.27 10:29"
  204. "2015.05.27 12:50"
  205.  
  206. "2015.05.27 15:02"
  207.  
  208. "2015.05.28 01:17"
  209.  
  210. "2015.05.28 18:00"
  211. "2015.06.01 15:32"
  212.  
  213. "2015.06.03 19:56"
  214.  
  215. "2015.06.06 15:53"
  216.  
  217. "2015.06.07 00:04"
  218. "2015.06.08 02:42"
  219.  
  220. "2015.06.08 18:51"
  221.  
  222. "2015.06.09 03:16"
  223.  
  224. "2015.06.13 02:42"
  225. "2015.06.14 01:17"
  226.  
  227. "2015.06.15 14:26"
  228.  
  229. "2015.06.23 14:02"
  230.  
  231. "2015.06.26 11:42"
  232. "2015.06.26 12:03"
  233.  
  234. "2015.06.26 23:39"
  235.  
  236. "2015.07.02 21:49"
  237.  
  238. "2015.07.09 17:18"
  239. "2015.07.11 15:19"
  240.  
  241. "2015.07.12 21:14"
  242.  
  243. "2015.07.12 21:18"
  244.  
  245. "2015.07.14 23:02"
  246. "2015.07.16 19:25"
  247.  
  248. "2015.07.17 17:33"
  249.  
  250. "2015.07.18 06:40"
  251.  
  252. "2015.07.23 13:10"
  253. "2015.07.23 13:45"
  254.  
  255. "2015.07.23 17:25"
  256.  
  257. "2015.07.23 22:22"
  258.  
  259. "2015.07.23 23:18"
  260. "2015.07.24 07:49"
  261.  
  262. "2015.07.23 22:23"
  263.  
  264. "2015.07.24 00:02"
  265.  
  266. "2015.07.24 01:57"
  267. "2015.07.24 03:12"
  268.  
  269. "2015.07.24 07:26"
  270.  
  271. "2015.07.24 09:19"
  272.  
  273. "2015.07.24 15:31"
  274. "2015.07.25 09:24"
  275.  
  276. "2015.08.06 11:32"
  277.  
  278. "2015.08.06 12:48"
  279.  
  280. "2015.08.09 19:31"
  281. "2015.08.09 21:49"
  282.  
  283. "2015.08.09 22:41"
  284.  
  285. "2015.08.10 08:42"
  286.  
  287. "2015.08.10 10:44"
  288. "2015.08.10 17:22"
  289.  
  290. "2015.08.12 01:44"
  291.  
  292. "2015.08.18 02:51"
  293.  
  294. "2015.08.18 04:11"
  295. "2015.08.18 10:13"
  296.  
  297. "2015.08.19 01:07"
  298.  
  299. "2015.08.23 06:01"
  300.  
  301. "2015.08.23 20:25"
  302. "2015.08.24 09:10"
  303.  
  304. "2015.08.28 21:00"
  305.  
  306. "2015.09.03 09:15"
  307.  
  308. "2015.09.03 09:15"
  309. "2015.09.03 09:16"
  310.  
  311. "2015.09.03 09:29"
  312.  
  313. "2015.09.03 13:28"
  314.  
  315. "2015.09.03 13:30"
  316. "2015.09.03 18:08"
  317.  
  318. "2015.09.03 18:11"
  319.  
  320. "2015.09.03 18:14"
  321.  
  322. "2015.09.08 17:41"
  323. "2015.09.10 10:57"
  324.  
  325. "2015.09.10 19:34"
  326.  
  327. "2015.09.12 14:59"
  328.  
  329. "2015.10.05 16:42"
  330. "2015.10.22 16:51"
  331.  
  332. "2015.10.23 09:15"
  333.  
  334. "2015.10.28 14:44"
  335.  
  336. "2015.10.28 14:48"
  337. "2015.11.03 15:15"
  338.  
  339. "2015.11.07 14:53"
  340.  
  341. "2015.12.06 14:52"
  342.  
  343. "2016.02.04 09:00"
  344. "2016.02.11 13:35"
  345.  
  346. "2016.02.12 04:05"
  347.  
  348. "2016.02.13 18:23"
  349.  
  350. "2016.02.15 21:40"
  351. "2016.02.16 00:41"
  352.  
  353. "2016.02.16 17:14"
  354.  
  355. "2016.02.20 19:32"
  356.  
  357. "2016.02.25 22:09"
  358. "2016.03.04 17:14"
  359.  
  360. "2016.04.02 21:44"
  • 시간을 제거하고 년/월 단위만 남기기
    • 데이터 타입을 Date 형식으로 바꿔줘야 이후 계산이 편함
In [14]:
date<-unlist(strsplit(blog$date," "))[1:length(blog$date)%%2==1]
head(date)
  1. "2015.04.18"
  2.  
  3. "2015.04.18"
  4.  
  5. "2015.04.18"
  6.  
  7. "2015.04.18"
  8.  
  9. "2015.04.18"
  10. "2015.04.18"
In [15]:
str(date)
 chr [1:206] "2015.04.18" "2015.04.18" "2015.04.18" "2015.04.18" ...
In [16]:
date<-as.Date(date,format("20%y.%m.%d"))
In [17]:
str(date)
 Date[1:206], format: "2015-04-18" "2015-04-18" "2015-04-18" "2015-04-18" ...
In [18]:
date<-format(date,"%Y-%m")
date
  1. "2015-04"
  2.  
  3. "2015-04"
  4.  
  5. "2015-04"
  6.  
  7. "2015-04"
  8.  
  9. "2015-04"
  10.  
  11. "2015-04"
  12.  
  13. "2015-04"
  14. "2015-04"
  15.  
  16. "2015-04"
  17.  
  18. "2015-04"
  19.  
  20. "2015-04"
  21.  
  22. "2015-04"
  23.  
  24. "2015-04"
  25.  
  26. "2015-04"
  27. "2015-04"
  28.  
  29. "2015-04"
  30.  
  31. "2015-04"
  32.  
  33. "2015-04"
  34.  
  35. "2015-04"
  36.  
  37. "2015-04"
  38.  
  39. "2015-04"
  40. "2015-04"
  41.  
  42. "2015-04"
  43.  
  44. "2015-04"
  45.  
  46. "2015-04"
  47.  
  48. "2015-04"
  49.  
  50. "2015-04"
  51.  
  52. "2015-04"
  53. "2015-04"
  54.  
  55. "2015-04"
  56.  
  57. "2015-04"
  58.  
  59. "2015-04"
  60.  
  61. "2015-04"
  62.  
  63. "2015-04"
  64.  
  65. "2015-04"
  66. "2015-04"
  67.  
  68. "2015-04"
  69.  
  70. "2015-04"
  71.  
  72. "2015-04"
  73.  
  74. "2015-04"
  75.  
  76. "2015-04"
  77.  
  78. "2015-04"
  79. "2015-04"
  80.  
  81. "2015-04"
  82.  
  83. "2015-04"
  84.  
  85. "2015-04"
  86.  
  87. "2015-04"
  88.  
  89. "2015-04"
  90.  
  91. "2015-04"
  92. "2015-04"
  93.  
  94. "2015-04"
  95.  
  96. "2015-04"
  97.  
  98. "2015-04"
  99.  
  100. "2015-04"
  101.  
  102. "2015-04"
  103.  
  104. "2015-04"
  105. "2015-04"
  106.  
  107. "2015-04"
  108.  
  109. "2015-04"
  110.  
  111. "2015-04"
  112.  
  113. "2015-04"
  114.  
  115. "2015-04"
  116.  
  117. "2015-04"
  118. "2015-04"
  119.  
  120. "2015-04"
  121.  
  122. "2015-04"
  123.  
  124. "2015-04"
  125.  
  126. "2015-04"
  127.  
  128. "2015-04"
  129.  
  130. "2015-04"
  131. "2015-04"
  132.  
  133. "2015-04"
  134.  
  135. "2015-04"
  136.  
  137. "2015-04"
  138.  
  139. "2015-04"
  140.  
  141. "2015-04"
  142.  
  143. "2015-04"
  144. "2015-05"
  145.  
  146. "2015-05"
  147.  
  148. "2015-05"
  149.  
  150. "2015-05"
  151.  
  152. "2015-05"
  153.  
  154. "2015-05"
  155.  
  156. "2015-05"
  157. "2015-05"
  158.  
  159. "2015-05"
  160.  
  161. "2015-05"
  162.  
  163. "2015-05"
  164.  
  165. "2015-05"
  166.  
  167. "2015-05"
  168.  
  169. "2015-05"
  170. "2015-05"
  171.  
  172. "2015-05"
  173.  
  174. "2015-05"
  175.  
  176. "2015-05"
  177.  
  178. "2015-05"
  179.  
  180. "2015-05"
  181.  
  182. "2015-05"
  183. "2015-05"
  184.  
  185. "2015-05"
  186.  
  187. "2015-05"
  188.  
  189. "2015-05"
  190.  
  191. "2015-05"
  192.  
  193. "2015-05"
  194.  
  195. "2015-05"
  196. "2015-05"
  197.  
  198. "2015-05"
  199.  
  200. "2015-05"
  201.  
  202. "2015-05"
  203.  
  204. "2015-05"
  205.  
  206. "2015-05"
  207.  
  208. "2015-05"
  209. "2015-05"
  210.  
  211. "2015-05"
  212.  
  213. "2015-05"
  214.  
  215. "2015-05"
  216.  
  217. "2015-05"
  218.  
  219. "2015-05"
  220.  
  221. "2015-05"
  222. "2015-05"
  223.  
  224. "2015-06"
  225.  
  226. "2015-06"
  227.  
  228. "2015-06"
  229.  
  230. "2015-06"
  231.  
  232. "2015-06"
  233.  
  234. "2015-06"
  235. "2015-06"
  236.  
  237. "2015-06"
  238.  
  239. "2015-06"
  240.  
  241. "2015-06"
  242.  
  243. "2015-06"
  244.  
  245. "2015-06"
  246.  
  247. "2015-06"
  248. "2015-06"
  249.  
  250. "2015-07"
  251.  
  252. "2015-07"
  253.  
  254. "2015-07"
  255.  
  256. "2015-07"
  257.  
  258. "2015-07"
  259.  
  260. "2015-07"
  261. "2015-07"
  262.  
  263. "2015-07"
  264.  
  265. "2015-07"
  266.  
  267. "2015-07"
  268.  
  269. "2015-07"
  270.  
  271. "2015-07"
  272.  
  273. "2015-07"
  274. "2015-07"
  275.  
  276. "2015-07"
  277.  
  278. "2015-07"
  279.  
  280. "2015-07"
  281.  
  282. "2015-07"
  283.  
  284. "2015-07"
  285.  
  286. "2015-07"
  287. "2015-07"
  288.  
  289. "2015-07"
  290.  
  291. "2015-07"
  292.  
  293. "2015-08"
  294.  
  295. "2015-08"
  296.  
  297. "2015-08"
  298.  
  299. "2015-08"
  300. "2015-08"
  301.  
  302. "2015-08"
  303.  
  304. "2015-08"
  305.  
  306. "2015-08"
  307.  
  308. "2015-08"
  309.  
  310. "2015-08"
  311.  
  312. "2015-08"
  313. "2015-08"
  314.  
  315. "2015-08"
  316.  
  317. "2015-08"
  318.  
  319. "2015-08"
  320.  
  321. "2015-08"
  322.  
  323. "2015-08"
  324.  
  325. "2015-09"
  326. "2015-09"
  327.  
  328. "2015-09"
  329.  
  330. "2015-09"
  331.  
  332. "2015-09"
  333.  
  334. "2015-09"
  335.  
  336. "2015-09"
  337.  
  338. "2015-09"
  339. "2015-09"
  340.  
  341. "2015-09"
  342.  
  343. "2015-09"
  344.  
  345. "2015-09"
  346.  
  347. "2015-09"
  348.  
  349. "2015-10"
  350.  
  351. "2015-10"
  352. "2015-10"
  353.  
  354. "2015-10"
  355.  
  356. "2015-10"
  357.  
  358. "2015-11"
  359.  
  360. "2015-11"
  361.  
  362. "2015-12"
  363.  
  364. "2016-02"
  365. "2016-02"
  366.  
  367. "2016-02"
  368.  
  369. "2016-02"
  370.  
  371. "2016-02"
  372.  
  373. "2016-02"
  374.  
  375. "2016-02"
  376.  
  377. "2016-02"
  378. "2016-02"
  379.  
  380. "2016-03"
  381.  
  382. "2016-04"
In [19]:
blog$date<-date
  • 토픽 비율 구하기
    • 수식

In [20]:
tot<-rowSums(trends)
In [21]:
trends
V1V2V3V4V5V6V7V8V9V10
10150000320
20030010450
30000006100
40000000420
500100031800
62401106002900
7001802300000
80020000000
9011009001300
100030406000
11000201124170130
1200006000110
130300290423320
14200336420230180
151000000020
160000010000
1700000000180
1800002106000
190000570010
200901300000
210405100000
22002018001210
230090000000
241000000300
2500014000933
2679000000000
270000300100
280060000000
290100000000
3000000000063
.................................
1770004000000
1780000000500
179009161000000
18000012500000
1810001000000
1820000000000
1830000600200
1840000900000
1850200001000
1860060000006
1870000003000
188000100000100
189501800002400
19004041401000
1910001000000
1920000005000
193000016016050
19400700170000
1950030000104
19600041700110
19725000070000
1980000002000
19900011600000
2000002500000
20100012300000
2020906700400
2030000600200
2040000006200
20500001100000
2060010000000
In [22]:
str(trends)
'data.frame':	206 obs. of  10 variables:
 $ V1 : int  0 0 0 0 0 24 0 0 0 0 ...
 $ V2 : int  1 0 0 0 0 0 0 0 11 0 ...
 $ V3 : int  5 3 0 0 1 11 18 2 0 3 ...
 $ V4 : int  0 0 0 0 0 0 0 0 0 0 ...
 $ V5 : int  0 0 0 0 0 6 23 0 9 4 ...
 $ V6 : int  0 1 0 0 0 0 0 0 0 0 ...
 $ V7 : int  0 0 6 0 3 0 0 0 0 6 ...
 $ V8 : int  3 4 1 4 18 29 0 0 13 0 ...
 $ V9 : int  2 5 0 2 0 0 0 0 0 0 ...
 $ V10: int  0 0 0 0 0 0 0 0 0 0 ...
In [23]:
trends<-trends/tot
  • 해당 변수를 상기 문서별 빈도 항목에 조인
In [24]:
for(i in 1:ncol(trends)){
    trends[,i][is.nan(trends[,i])]<-0
}
In [25]:
trends$date<-blog$date
In [26]:
colnames(trends)<-c(paste("Topic",1:10),"date")
In [27]:
str(trends)
'data.frame':	206 obs. of  11 variables:
 $ Topic 1 : num  0 0 0 0 0 ...
 $ Topic 2 : num  0.0909 0 0 0 0 ...
 $ Topic 3 : num  0.4545 0.2308 0 0 0.0455 ...
 $ Topic 4 : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Topic 5 : num  0 0 0 0 0 ...
 $ Topic 6 : num  0 0.0769 0 0 0 ...
 $ Topic 7 : num  0 0 0.857 0 0.136 ...
 $ Topic 8 : num  0.273 0.308 0.143 0.667 0.818 ...
 $ Topic 9 : num  0.182 0.385 0 0.333 0 ...
 $ Topic 10: num  0 0 0 0 0 0 0 0 0 0 ...
 $ date    : chr  "2015-04" "2015-04" "2015-04" "2015-04" ...
In [28]:
trends_month<-aggregate(trends,list(trends$date),FUN=mean)
Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"Warning message in mean.default(X[[i]], ...):
"argument is not numeric or logical: returning NA"
In [29]:
trends_month
Group.1Topic 1Topic 2Topic 3Topic 4Topic 5Topic 6Topic 7Topic 8Topic 9Topic 10date
12015-040.07939689048938840.05283911579398270.1367162368766280.07528504057869720.1969780933266530.05371357277572590.07767652073016710.09820458320403270.09704281832447260.132147127900253NA
22015-050.05703737539636070.02433276522789470.07445644422878370.09224552565247870.2013791060665060.06389041237667450.08575034986192040.1328546281522550.1094072671117340.135390311971904NA
32015-060.1151254480286740.09012820512820510.09526901669758810.007936507936507940.2458951954343660.04802867383512540.1353195823784060.07142857142857140.1348463901689710.0560224089635854NA
42015-070.1024844720496890.02157573174197220.1325392783881140.0495804729214340.2007785509236580.08734377750396060.04834782608695650.1715054967915380.08990961098398170.0524565217391304NA
52015-080.04573325416708040.079346018807430.06216579031430210.03231263000905120.3565772222605760.04613265939254530.05345022624434390.1253943298398520.09520044527739490.103687423687424NA
62015-0900.05128205128205130.05824175824175820.3202327084680030.1792178409825470.05769230769230770.1025641025641030.09615384615384620.01923076923076920.0384615384615385NA
72015-100.02127659574468090.03478260869565220.07659574468085110.3347826086956520.12173913043478300.2086956521739130.1021276595744680.10NA
82015-11000.14583333333333300.2162162162162160.3541666666666670.21621621621621600.06756756756756760NA
92015-12000.37500000.12500.5NA
102016-020.08680555555555560.038461538461538500.08787630622668990.4085005053675130.05812198067632850.1944444444444440.07264957264957270.05314009661835750NA
112016-030000100000NA
122016-040010000000NA
In [30]:
trends_month$date<-NULL
In [31]:
str(trends_month)
'data.frame':	12 obs. of  11 variables:
 $ Group.1 : chr  "2015-04" "2015-05" "2015-06" "2015-07" ...
 $ Topic 1 : num  0.0794 0.057 0.1151 0.1025 0.0457 ...
 $ Topic 2 : num  0.0528 0.0243 0.0901 0.0216 0.0793 ...
 $ Topic 3 : num  0.1367 0.0745 0.0953 0.1325 0.0622 ...
 $ Topic 4 : num  0.07529 0.09225 0.00794 0.04958 0.03231 ...
 $ Topic 5 : num  0.197 0.201 0.246 0.201 0.357 ...
 $ Topic 6 : num  0.0537 0.0639 0.048 0.0873 0.0461 ...
 $ Topic 7 : num  0.0777 0.0858 0.1353 0.0483 0.0535 ...
 $ Topic 8 : num  0.0982 0.1329 0.0714 0.1715 0.1254 ...
 $ Topic 9 : num  0.097 0.1094 0.1348 0.0899 0.0952 ...
 $ Topic 10: num  0.1321 0.1354 0.056 0.0525 0.1037 ...
  • 시각화 하기
In [32]:
library(ggplot2)
p<-ggplot(trends_month,aes(x=Group.1))
trends_plot<-p+geom_line(aes(y=`Topic 1`,group=1),color="red")+
  geom_line(aes(y=`Topic 2`,group=1),color="brown")+
  geom_line(aes(y=`Topic 3`,group=1),color="blue")+
  geom_line(aes(y=`Topic 4`,group=1),color="yellow")+
  geom_line(aes(y=`Topic 5`,group=1),color="green")+
  geom_line(aes(y=`Topic 6`,group=2),color="orange")+
  geom_line(aes(y=`Topic 7`,group=2),color="purple")+
  geom_line(aes(y=`Topic 8`,group=2),color="aquamarine1")+
  geom_line(aes(y=`Topic 9`,group=2),color="black")+
  geom_line(aes(y=`Topic 10`,group=2),color="cyan")+
  ylab("Topics") + xlab("")+ ggtitle("Topic Trends")
Attaching package: 'ggplot2'

The following object is masked from 'package:NLP':

    annotate

In [33]:
trends_plot


Archives

05-16 06:06

Contact Us

Address
경기도 수원시 영통구 원천동 산5번지 아주대학교 다산관 429호

E-mail
textminings@gmail.com

Phone
031-219-2910

Tags

Calendar

«   2024/05   »
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
Copyright © All Rights Reserved
Designed by CMSFactory.NET