합성 데이터 만들기
합성 데이터 만들기 (Creating Synthetic Data)
DuckDB를 쓰면 합성(synthetic) 데이터셋을 빠르게 만들 수 있어요. 다음 방법들을 사용할 수 있어요:
출처: 공식문서
- range 함수
- 해시 함수, 예를 들면
hash,md5,sha256 - Python 함수 API를 통한 Faker Python 패키지
- 크로스 곱 (카테시안 곱) 사용
예시
예를 들면:
import duckdb
from duckdb.sqltypes import *
from faker import Faker
fake = Faker()
def random_date():
return fake.date_between()
def random_short_text():
return fake.text(max_nb_chars=20)
def random_long_text():
return fake.text(max_nb_chars=200)
con = duckdb.connect()
con.create_function("random_date", random_date, [], DATE, type="native", side_effects=True)
con.create_function("random_short_text", random_short_text, [], VARCHAR, type="native", side_effects=True)
con.create_function("random_long_text", random_long_text, [], VARCHAR, type="native", side_effects=True)
res = con.sql("""
SELECT
hash(i * 10 + j) AS id,
random_date() AS creationDate,
random_short_text() AS short,
random_long_text() AS long,
IF (j % 2, true, false) AS bool
FROM generate_series(1, 5) s(i)
CROSS JOIN generate_series(1, 2) t(j)
""")
res.show()
이렇게 하면 다음 결과가 생성돼요:
┌──────────────────────┬──────────────┬─────────┐
│ id │ creationDate │ flag │
│ uint64 │ date │ boolean │
├──────────────────────┼──────────────┼─────────┤
│ 6770051751173734325 │ 2019-11-05 │ true │
│ 16510940941872865459 │ 2002-08-03 │ true │
│ 13285076694688170502 │ 1998-11-27 │ true │
│ 11757770452869451863 │ 1998-07-03 │ true │
│ 2064835973596856015 │ 2010-09-06 │ true │
│ 17776805813723356275 │ 2020-12-26 │ false │
│ 13540103502347468651 │ 1998-03-21 │ false │
│ 4800297459639118879 │ 2015-06-12 │ false │
│ 7199933130570745587 │ 2005-04-13 │ false │
│ 18103378254596719331 │ 2014-09-15 │ false │
├──────────────────────┴──────────────┴─────────┤
│ 10 rows 3 columns │
└───────────────────────────────────────────────┘