지연 파이프라인 구축

Python에서 Dask로 병렬 프로그래밍

James Fulton

Climate Informatics Researcher

데이터 청크

데이터셋이 하드 드라이브에는 맞지만 RAM에는 너무 큰 상황을 보여주는 다이어그램.

데이터셋을 여러 조각으로 나눈 다이어그램. 전체는 RAM에 너무 크지만, 각 조각은 RAM에 적합함.

Python에서 Dask로 병렬 프로그래밍

Spotify 노래 데이터셋

files = [
  '2005_tracks.csv',
  '2006_tracks.csv',
  '2007_tracks.csv',
  '2008_tracks.csv',
  '2009_tracks.csv',
  '2010_tracks.csv',
  ...
  '2020_tracks.csv',
]
Python에서 Dask로 병렬 프로그래밍

Spotify 노래 데이터셋

                       name  duration_ms release_date  ...
0     Aldrig (feat. Carmon)       247869   2019-01-01  ...
2  2019 - The Year to Build       288105   2019-01-01  ...
3                 Na zawsze       186812   2019-01-01  ...
4         Humo en la Trampa       258354   2019-01-01  ...
5                     Au Au       176000   2019-01-01  ...
...                     ...          ...          ...  ...
Python에서 Dask로 병렬 프로그래밍

데이터 분석

import pandas as pd

maximums = []

for file in files:
    # Load each file
    df = pd.read_csv(file)

# Find maximum track length in each file max_length = df['duration_ms'].max()
# Store this maximum maximums.append(max_length)
# Find the maximum of all the maximum lengths absolute_maximum = max(maximums)
Python에서 Dask로 병렬 프로그래밍

데이터 분석

import pandas as pd

maximums = []

for file in files:
    # Load each file
    df = delayed(pd.read_csv)(file) # <------- delay loading
    # Find maximum track length in each file
    max_length = df['duration_ms'].max()
    # Store this maximum
    maximums.append(max_length)

# Find the maximum of all the maximum lengths
absolute_maximum = max(maximums)
Python에서 Dask로 병렬 프로그래밍

데이터 분석

import pandas as pd

maximums = []

for file in files:
    # Load each file
    df = delayed(pd.read_csv)(file) # <------- delay loading
    # Find maximum track length in each file
    max_length = df['duration_ms'].max()
    # Store this maximum
    maximums.append(max_length)

# Find the maximum of all the maximum lengths
absolute_maximum = delayed(max)(maximums) # <------- delay max() function
Python에서 Dask로 병렬 프로그래밍

지연 객체의 메서드 사용

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file)
    # Use the .max() method
    max_length = df['duration_ms'].max()

    maximums.append(max_length)


absolute_maximum = delayed(max)(maximums)
print(max_length)
Delayed('max-0602855d-3ee6-4c43-a4d2-...')
  • 지연 객체의 메서드/속성은 새로운 지연 객체를 반환합니다
print(df.shape)
print(df.shape.compute())
Delayed('getattr-bc1e8838ab...')
(11907, 12)
Python에서 Dask로 병렬 프로그래밍

지연 객체의 메서드 사용

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file)
    # Use a method which doesn't exist
    max_length = df['duration_ms'].fake()

    maximums.append(max_length)


absolute_maximum = delayed(max)(maximums)
print(max_length)
Delayed('max-6c026036-5daf-4b2-...')
  • .compute()를 호출하기 전까지 메서드는 실행되지 않습니다
print(max_length.compute())
...
AttributeError: 'Series' object has no 
attribute 'fake'
Python에서 Dask로 병렬 프로그래밍

지연 객체의 메서드 사용

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file)

    max_length = df['duration_ms'].max()
    # Add delayed object to list
    maximums.append(max_length)

# Run delayed max on delayed objects list
absolute_maximum = delayed(max)(maximums)

maximums는 지연 객체 리스트입니다

print(maximums)
[Delayed('max-80b...'), 
Delayed('max-fa15d...', 
...]
Python에서 Dask로 병렬 프로그래밍

지연 객체 리스트 계산하기

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file)

    max_length = df['duration_ms'].max()
    # Add dalayed object to list
    maximums.append(max_length)

# Compute all the maximums
all_maximums = dask.compute(maximums)
print(all_maximums)
([2539418, 4368000, ...
... 4511716, 4864333],)
Python에서 Dask로 병렬 프로그래밍

지연 객체 리스트 계산하기

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file) 

    max_length = df['duration_ms'].max()

    maximums.append(max_length)

# Compute all the maximums
all_maximums = dask.compute(maximums)[0]
print(all_maximums)
[2539418, 4368000, ...
... 4511716, 4864333]
Python에서 Dask로 병렬 프로그래밍

지연할지 말지

def get_max_track(df):
    return df['duration_ms'].max()

for file in files:
    df = delayed(pd.read_csv)(file) 
    # Use function to find max
    max_length = get_max_track(df)

    maximums.append(max_length)


absolute_maximum = delayed(max)(maximums)
Python에서 Dask로 병렬 프로그래밍

더 깊은 작업 그래프

absolute_maximum.visualize()

파일별 최대값의 최댓값 계산 단계(큰지만 단순한) 작업 그래프.

Python에서 Dask로 병렬 프로그래밍

Ayo berlatih!

Python에서 Dask로 병렬 프로그래밍

Preparing Video For Download...