Main Content

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

동시발생 신경망 만들기

이 예제에서는 bag-of-words 모델을 사용하여 동시발생 신경망을 만드는 방법을 보여줍니다.

동시발생 신경망은 문서의 말뭉치가 주어지면 무방향 그래프가 되어 노드는 단어집에 있는 고유한 단어, 간선은 문서에서 동시에 발생하는 단어의 빈도에 해당하게 됩니다. 동시발생 신경망을 사용하면 문서의 말뭉치 내에 있는 단어 간의 관계 정보를 시각화하고 추출할 수 있습니다. 예를 들어 동시발생 신경망을 사용하면 지정된 단어와 함께 주로 나타나는 단어를 찾을 수 있습니다.

텍스트 데이터 가져오기

readtable을 사용하여 파일 weekendUpdates.xlsx에서 텍스트 데이터를 추출합니다. 파일 weekendUpdates.xlsx에는 해시태그 "#weekend""#vacation"이 포함된 상태 업데이트가 들어 있습니다. readtable 함수를 사용하여 데이터를 읽어온 후 TextData 열에서 텍스트 데이터를 추출합니다.

filename = "weekendUpdates.xlsx";
tbl = readtable(filename,'TextType','string');
textData = tbl.TextData;

처음 몇 개의 관측값을 표시합니다.

textData(1:5)
ans = 5x1 string
    "Happy anniversary! ❤ Next stop: Paris! ✈ #vacation"
    "Haha, BBQ on the beach, engage smug mode!   ❤  #vacation"
    "getting ready for Saturday night  #yum #weekend "
    "Say it with me - I NEED A #VACATION!!! ☹"
    " Chilling  at home for the first time in ages…This is the life!  #weekend"

텍스트 데이터 전처리하기

텍스트를 토큰화하고 토큰화된 텍스트를 소문자로 변환한 후 불용어를 제거합니다.

documents = tokenizedDocument(textData);

documents = lower(documents);
documents = removeStopWords(documents);

bag-of-words 모델을 사용하여 단어 개수로 구성된 행렬을 만듭니다.

bag = bagOfWords(documents);
counts = bag.Counts;

단어 개수 행렬에 그 전치를 곱하여 단어의 동시발생을 계산합니다.

cooccurrence = counts.'*counts;

graph 함수를 사용하여 동시발생 행렬을 신경망으로 변환합니다.

G = graph(cooccurrence,bag.Vocabulary,'omitselfloops');

plot 함수를 사용하여 신경망을 시각화합니다. 선 두께를 간선 가중치의 배수로 설정합니다.

LWidths = 5*G.Edges.Weight/max(G.Edges.Weight);

plot(G,'LineWidth',LWidths)
title("Co-occurence Network")

Figure contains an axes object. The axes object with title Co-occurence Network contains an object of type graphplot.

neighbors 함수를 사용하여 단어 "great"의 이웃을 찾습니다.

word = "great"
word = 
"great"
idx = find(bag.Vocabulary == word);
nbrs = neighbors(G,idx);
bag.Vocabulary(nbrs)'
ans = 18x1 string
    "next"
    "#vacation"
    ""
    "#weekend"
    "☹"
    "excited"
    "flight"
    "delayed"
    "stuck"
    "airport"
    "way"
    "spend"
    ""
    "lovely"
    "friends"
    "-"
    "mini"
    "everybody"

이 단어와 해당 이웃의 부분 그래프를 추출하여 단어 "great"의 동시발생을 시각화합니다.

H = subgraph(G,[idx; nbrs]);

LWidths = 5*H.Edges.Weight/max(H.Edges.Weight);
plot(H,'LineWidth',LWidths)
title("Co-occurence Network - Word: """ + word + """");

Figure contains an axes object. The axes object with title Co-occurence Network - Word: "great" contains an object of type graphplot.

그래프와 신경망 분석에 대한 자세한 내용은 그래프와 네트워크 알고리즘 항목을 참조하십시오.

참고 항목

| | |

관련 항목