Building Rich, High Performance Tools for Prac7cal Data - - PowerPoint PPT Presentation

building rich high performance tools for prac7cal data
SMART_READER_LITE
LIVE PREVIEW

Building Rich, High Performance Tools for Prac7cal Data - - PowerPoint PPT Presentation

Building Rich, High Performance Tools for Prac7cal Data Analysis Wes McKinney @wesmckinn Lambda Foundry, Inc. Talk Overview Background Goals Key Ingredients Examples My


slide-1
SLIDE 1

Building ¡Rich, ¡High ¡ Performance ¡Tools ¡for ¡ Prac7cal ¡Data ¡Analysis

Wes ¡McKinney @wesmckinn Lambda ¡Foundry, ¡Inc.

slide-2
SLIDE 2

Talk ¡Overview

  • Background
  • Goals
  • Key ¡Ingredients
  • Examples
slide-3
SLIDE 3

My ¡Background

  • MIT ¡’07, ¡ ¡AQR ¡2007-­‑2010, ¡ ¡LF ¡2012-­‑ ¡
  • Lead ¡developer ¡of ¡pandas ¡ ¡(Python ¡library)
slide-4
SLIDE 4

Goals

Data ¡Tooling

slide-5
SLIDE 5

Goals

Big™ ¡and ¡Small™ ¡Data

slide-6
SLIDE 6
slide-7
SLIDE 7

Goals

Simplify ¡Data ¡Wrangling

slide-8
SLIDE 8

Goals

User ¡interface ¡design

slide-9
SLIDE 9

User ¡interface ¡design

i.e. ¡“how ¡do ¡I ¡do ¡what ¡ I ¡need?”

slide-10
SLIDE 10
slide-11
SLIDE 11

API ¡Design

slide-12
SLIDE 12

API ¡Design

  • “Fits ¡your ¡brain”
  • “All ¡my ¡funcPons ¡have ¡17 ¡arguments”
slide-13
SLIDE 13

Syntax Matters

slide-14
SLIDE 14

API ¡Design

(($:@(<#[),(=#[),$:@(>#[))({~ ?@#))^:(1<#)

{f:*x@1?#x;:[0=#x;x;,/(_f x@&x<f;x@&x=f;_f x@&x>f)]}

Text Text
slide-15
SLIDE 15

API ¡Design

(($:@(<#[),(=#[),$:@(>#[))({~ ?@#))^:(1<#)

{f:*x@1?#x;:[0=#x;x;,/(_f x@&x<f;x@&x=f;_f x@&x>f)]}

Text Text

J K/Kona

slide-16
S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]>>>>>>[<+<+>>-]<[>+<-]<<<[>+>[<-]<[<]>>[<<+>[-]+>-]>-<<- ]>>[-]+<<<[->>+<<]>>[->-<<<<<[>+<-]<[>+<-]>>>>>>>>[<<<<<<<<+>>>>>>>>-] <<]>[[-]<<<<<<[>>+>>>>>+<<<<<<<-]>>[<<+>>-]>>>>>[-[>>[<<<+>>>-]<[>+<-] <-[>+<-]>]<<[[>>+<<-]<]]>]<<<<<<-]>[>>>>>>+<<<<<<-]<<[[>>>>>>>+<<<<<<<

  • ]>[<+>-]<+<]<[[>>>>>>>>+<<<<<<<<-]>>[<+>-]<+<<]>+>[<-<<[>+<-]<[<]>[[<

+>-]>]>>>[<<<<+>>>>-]<<[<+>-]>>]<[-<<+>>]>>>]<<<<<<]>>>>>>>>>>>[.>]

Brainf***

slide-18
SLIDE 18

“A ¡user ¡interface ¡can ¡handle ¡only ¡so ¡ much ¡complexity ¡or ¡it ¡becomes ¡ unusable.” Guido ¡van ¡Rossum

slide-19
SLIDE 19
slide-20
SLIDE 20

Key ¡Ingredients

  • API ¡/ ¡User ¡interface ¡design ¡
  • Data ¡types
  • Arrays
  • Data ¡structures
  • Efficient ¡Algorithms
slide-21
SLIDE 21

Arrays

  • Blob ¡of ¡bytes ¡as ¡mulPdimensional ¡array
  • All ¡elements ¡same ¡type
  • ImplementaPons: ¡NumPy ¡(Python), ¡J ¡/ ¡APL, ¡R, ¡MATLAB, ¡...

Scalar Rank ¡0 Vector Rank ¡1 Matrix Rank ¡2 Cube/Hypercube Rank ¡> ¡2

slide-22
SLIDE 22

Arrays

  • Loopless ¡programming
  • ¡VectorizaPon
  • ¡BroadcasPng
  • No-­‑copy ¡views
slide-23
SLIDE 23

(Some) ¡Basic ¡data ¡types

  • Integer
  • FloaPng ¡point ¡/ ¡Complex
  • Characters ¡and ¡Strings ¡(ASCII ¡or ¡Unicode)
  • Date ¡and ¡Pme
  • Box ¡/ ¡“Object”
slide-24
SLIDE 24

(Some) ¡Basic ¡data ¡types

  • Integer
  • FloaPng ¡point ¡/ ¡Complex
  • Characters ¡and ¡Strings ¡(ASCII ¡or ¡Unicode)
  • Date ¡and ¡Pme
  • Box ¡/ ¡“Object”
  • Categorical ¡/ ¡enumeraPon ¡(R: ¡“factors”)
  • Record ¡or ¡structure ¡(one ¡or ¡more ¡of ¡the ¡above)
slide-25
SLIDE 25

Data ¡types

Text

Also, ¡any ¡values ¡can ¡be ¡“missing” ¡(NA)

slide-26
SLIDE 26

Risks

  • Stuck ¡in ¡type ¡soup

generic number character integer floating bool_ unsigned int signed int string_ unicode_ complex inexact

  • bject_
slide-27
SLIDE 27

Risks

  • Actually ¡it’s ¡more ¡like

integer unsigned int signed int uint8 uint16 uint32 uint64 int8 int16 int32 int64

slide-28
SLIDE 28

Data ¡types ¡and ¡users

  • Control ¡over ¡machine ¡representaPon
  • Treacherous, ¡but ¡oben ¡necessary
  • Simplicity ¡vs. ¡power ¡and ¡control
slide-29
SLIDE 29

Tables

  • A ¡sequence ¡of ¡arrays, ¡each ¡with ¡own ¡data ¡type

1 2 3 4 5

slide-30
SLIDE 30

Tables

  • A ¡sequence ¡of ¡arrays, ¡each ¡with ¡own ¡data ¡type
  • Common ¡simplificaPon: ¡only ¡1-­‑dimensional ¡arrays

1 2 3 4 5

slide-31
SLIDE 31

Table ¡and ¡Array ¡Axis ¡Labeling

  • One ¡or ¡more ¡categorical ¡arrays ¡per ¡axis
  • Numerous ¡uses

1 2 3 4 5

1 2 3 4 1 2 3 4

A B

Text

“Row labels”

slide-32
SLIDE 32

Table ¡and ¡Array ¡Axis ¡Labeling

slide-33
SLIDE 33

Table ¡and ¡Array ¡Axis ¡Labeling

....

slide-34
SLIDE 34

This ¡predicates ¡on ¡the ¡ existence ¡of ¡sane ¡APIs ¡

slide-35
SLIDE 35

Some ¡“primi7ve” ¡opera7ons

  • Align ¡/ ¡join ¡/ ¡merge ¡/ ¡“Vlookup”
  • Concatenate
  • Reshape, ¡Stack/fold, ¡Unstack/unfold, ¡Pivot, ¡Melt
  • Subset: ¡Drop ¡rows, ¡columns
  • Elementwise ¡transforms: ¡map, ¡replace, ¡fill, ¡string ¡stuff
  • Group ¡By: ¡Apply, ¡Aggregate, ¡Transform, ¡Cut, ¡...
  • Set ¡operaPons: ¡Unique, ¡deduplicate, ¡is-­‑in
slide-36
SLIDE 36

Example: ¡concatena7on

leb ¡(prices) right ¡(volume)

slide-37
SLIDE 37

Example: ¡concatena7on

result = concat([left, right], axis=1, keys=[‘price’, ‘volume’])

slide-38
SLIDE 38

Example: ¡stack/fold

result.stack(0)

slide-39
SLIDE 39

Under ¡the ¡hood

  • Array ¡/ ¡vector ¡operaPons
  • Efficient ¡data ¡movement
  • Hash ¡sets ¡and ¡tables
  • SorPng ¡algorithms
  • RelaPonal ¡algebra
slide-40
SLIDE 40

Example: ¡group-­‑by

  • H. ¡Wickham ¡“The ¡Split-­‑Apply-­‑Combine ¡Strategy ¡for ¡Data ¡Analysis”
  • Beyond ¡relaPonal ¡databases

SELECT key1, key2, key3, MEAN(value1), STD(value2) FROM table GROUP BY key1, key2, key2

slide-41
SLIDE 41

Example: ¡group-­‑by

  • In ¡pseudocode

grouped = table.groupby(key_list) result = grouped.apply(function)

slide-42
SLIDE 42

Example: ¡group-­‑by

  • In ¡pseudocode

grouped = table.groupby(key_list) result = grouped.apply(function) Arrays, functions, column names, ...

slide-43
SLIDE 43

Example: ¡group-­‑by

  • In ¡pseudocode

grouped = table.groupby(key_list) result = grouped.apply(function) Arrays, functions, column names, ...

Preferably, any function accepting an array or table

slide-44
SLIDE 44

Example: ¡group-­‑by

  • In ¡pseudocode

grouped = table.groupby(key_list) result = grouped.apply(function) Arrays, functions, column names, ...

Preferably, any function accepting an array or table

Something useful?

slide-45
SLIDE 45

Example: ¡group-­‑by

  • In ¡pictures

A B 5 C 10 5 10 15 10 15 20 A A A B B B C C C A 15 B 30 C 45 A B C A B C 5 10 5 10 15 10 15 20

sum

Apply Split key Combine

sum sum

data

slide-46
SLIDE 46

....

by_size = table.groupby(‘size’) by_size.apply(topn, ‘tip_pct’, n=2)

slide-47
SLIDE 47

cuts = cut(table.size, [0, 3, 6]) table.groupby(cuts).apply(topn, 'tip_pct', n=4)

slide-48
SLIDE 48

GroupBy: ¡inside

  • Key ¡arrays ¡-­‑> ¡Categorical ¡variables ¡(hash ¡table ¡pass)
  • Apply ¡step
  • ¡Simple ¡funcPons: ¡1 ¡pass ¡through ¡data
  • ¡Generally: ¡sort ¡data ¡(O(N)!), ¡iterate ¡through ¡chunks
  • Combine: ¡don’t ¡lose ¡informaPon!
slide-49
SLIDE 49

GroupBy: ¡inside

  • Key ¡arrays ¡-­‑> ¡Categorical ¡variables ¡(hash ¡table ¡pass)
  • Apply ¡step
  • ¡Simple ¡funcPons: ¡1 ¡pass ¡through ¡data
  • ¡Generally: ¡sort ¡data ¡(O(N)!), ¡iterate ¡through ¡chunks
  • Combine: ¡don’t ¡lose ¡informaPon!
  • Bonus: ¡precompute ¡(all ¡the ¡things)
slide-50
SLIDE 50

Sources ¡of ¡performance ¡/ ¡efficiency

  • OpPmal ¡algorithms
  • Cache-­‑efficient ¡memory ¡layout
  • “Boxing” ¡overhead
  • Minimal ¡copying ¡of ¡data
  • EliminaPon ¡of ¡temporary ¡variables
slide-51
SLIDE 51

Not ¡all ¡algo ¡impls ¡created ¡equal

  • Example: ¡Unique
  • Test ¡case: ¡1M ¡integers ¡with ¡10K ¡unique ¡values
slide-52
SLIDE 52

Not ¡all ¡algo ¡impls ¡created ¡equal

  • Example: ¡Unique
  • Test ¡case: ¡1M ¡integers ¡with ¡10K ¡unique ¡values
  • Shootout
  • R: ¡17.8 ¡ms
  • Python-­‑NumPy: ¡24.4 ¡ms
  • Python-­‑pandas: ¡10.1 ¡ms
  • Julia: ¡170 ¡ms ¡(Set(values))
slide-53
SLIDE 53

Not ¡all ¡algo ¡impls ¡created ¡equal

  • Example: ¡Unique
  • Test ¡case: ¡1M ¡integers ¡with ¡10K ¡unique ¡values
  • Shootout
  • R: ¡17.8 ¡ms
  • Python-­‑NumPy: ¡24.4 ¡ms
  • Python-­‑pandas: ¡10.1 ¡ms
  • Julia: ¡170 ¡ms ¡(Set(values)) ¡
  • J: ¡2.2 ¡ms ¡ ¡ ¡(~. ¡values)
slide-54
SLIDE 54

Examples

slide-55
SLIDE 55

TL;DR

slide-56
SLIDE 56

Thanks! On ¡TwiQer: ¡@wesmckinn