본문 바로가기

Papers/Implement

[python] 그래프 데이터 만들기(using. Snap)

728x90
반응형

파이썬을 이용하여 weight Labeled Graph Data를 만들어 보았다.

 

먼저, Graph data는 Snap Data를 사용하였다. Snap Data는 id_1, id_2 로 이루어진 데이터 이고, 이 데이터를 가공하여 weight Labeled Graph Data 를 만들 것이다. Snap data를 받을 수 있는 곳은 게시물 하단에 Link를 걸어둘테니, 필요하다면 해당 Link에 들어가서 Download 받기를 바란다.


 

데이터 가공 순서로는 snap data를 읽고, Weight와 그래프 Label을 임의적으로 생성을 하고, 새로운 File에 저장하는 형태로 진행하였다.

 

[ Source Code ]

 

""" 1. make a graph data file """

import random

random.seed(100)
with open('fileName.txt','r',encoding='utf-8') as f:
    lines = f.readlines()
    ids = []
    labels = ['A', 'B', 'C', 'D', 'E']
    for line in lines:
        ids = list(map(str, line.split()))
        ids.append(str(round(random.random(),1)))
        ids.append("".join(random.choices(labels)))
        ids.append("".join(random.choices(labels)))
        # print(ids)
        # break
        with open('NewWriteFileName.txt','a') as fr:
            fr.write(" ".join(ids))
            fr.write("\n")

 

위의 코드로 graph data를 생성하면 하나의 vertex id에서 여러개의 Label 을 가지게 된다. 따라서, vertex id 당 하나의 Label만 가질 수 있도록 조건처리를 해야 한다.

아래 코드는 위의 코드에서 조건처리를 한 코드이다. nc라는 빈 dictionary를 만들어 준 뒤, nc에 id(key)가 있으면, 해당 Label(value)가 나올 수 있도록 조건 처리를 진행하였다.

import random

random.seed(90)
with open('facebook_combined.txt','r',encoding='utf-8') as f:
    lines = f.readlines()
    nc = {}
    ids = []
    labels = ['A', 'B', 'C', 'D', 'E']
    for line in lines:
        ids = list(map(str, line.split()))
        ids.append(str(round(random.random(),1)))
        if ids[0] in nc:
            ids.append(nc[ids[0]])
        else:
            nc[ids[0]] = "".join(random.choices(labels))
            ids.append(nc[ids[0]])
        if ids[1] in nc:
            ids.append(nc[ids[1]])
        else:
            nc[ids[1]] = "".join(random.choices(labels))
            ids.append(nc[ids[1]])

 

 

 

참고로, python 파일명을 한글로 생성을 한 후 실행을 하니, 

PermissionError: [Errno 13] Permission denied

라는 오류문구가 발생하였는데, 파일명을 영어로 바꾸니 오류가 없어졌다.

또 다시 동일한 에러가 발생해서 이번에는 random.seed 함수를 바꿔 주었더니 오류가 없어졌다...

 

정확한 오류 발생근원을 찾아서 해결한 것 같지는 않지만,,, 계속해서 발생한다면 근본적으로 해결할 코드를 찾아봐야 할 것 같다... (지금은 시간이 너무 부족...)

 

오류 원인으로는 파일의 권한 문제 또는 파일 경로가 잘못되었을 때 발생한다고 한다.

본인은 해결하기 위하여 다음과 같은 순서로 진행하였다.

1. open 함수의 경로를 절대경로로 수정 => 오류 발생

2. .py 파일 영문이름으로 변경 => 오류 발생

3. random.seed() 를 변경 => 오류 발생 X


snap.stanford.edu/data/ego-Facebook.html

 

SNAP: Network datasets: Social circles

Social circles: Facebook Dataset information This dataset consists of 'circles' (or 'friends lists') from Facebook. Facebook data was collected from survey participants using this Facebook app. The dataset includes node features (profiles), circles, and eg

snap.stanford.edu

 

728x90
반응형

'Papers > Implement' 카테고리의 다른 글

dblp v10 json 파일 읽는 방법  (0) 2021.09.06
[python] node(vertex) topology index 만들기  (8) 2021.04.15