things i wish i knew before using python for data
play

Things I wish I Knew Before Using Python for Data Processing - PowerPoint PPT Presentation

Things I wish I Knew Before Using Python for Data Processing Miguel Cabrera @mfcabrera 20 July 2016 Hello! I am Miguel! Data Engineer / Scien6st @ TrustYou Python ~ 2 years Berlin


  1. Extra goodies >>> ¡c ¡= ¡Counter(a= 3, ¡b=1) ¡ >>> ¡d ¡= ¡Counter(a= 1, ¡b=2) ¡ >>> ¡c.most_common() ¡ ¡ >>> ¡c.values() ¡ >>> ¡c ¡+ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ >>> ¡c ¡-­‑ ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ >>> ¡c ¡& ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡intersection: ¡ ¡min(c[x], ¡ d[x]) ¡ >>> ¡c ¡| ¡d ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡union: ¡ ¡max(c[x], ¡d[x]) ¡ 55

  2. Counter is a class ¡ 56

  3. Classes can be extended ¡ 57

  4. Counter based PMF from ¡collections ¡import ¡Counter ¡ ¡ ¡ class ¡PMF(Counter): ¡ ¡ ¡ ¡ ¡ def ¡normalize(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡total ¡= ¡float(sum(self.values())) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ for ¡key ¡in ¡self: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self[key] ¡/= ¡total ¡ 58

  5. Counter based PMF from ¡collections ¡import ¡Counter ¡ ¡ ¡ class ¡PMF(Counter): ¡ ¡ ¡ ¡ ¡ def ¡normalize(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡total ¡= ¡float(sum(self.values())) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ for ¡key ¡in ¡self: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self[key] ¡/= ¡total ¡ ¡ ¡ ¡ ¡ ¡ def ¡__init__(self, ¡ *args, ¡**kwargs) : ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡super(PMF, ¡self).__init__(…) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.normalize() ¡ 59

  6. On Counting and Python ● Use ¡the ¡and ¡extend ¡ Counter ¡ class ¡ ● Awesome ¡ar6cle ¡from ¡ @TreyHunner ¡on ¡Coun6ng ¡[1]. ¡ ¡ ¡ 60 [1] http://treyhunner.com/2015/11/counting-things-in-python/

  7. Named Tuples 61

  8. Named Tuples ● Code ¡around ¡the ¡ dict , ¡ tuples ¡or ¡ lists ¡ ● Never ¡know ¡what ¡to ¡expect ¡ ● Code ¡becomes ¡hard ¡to ¡read ¡ ¡ 62

  9. Example from ¡math ¡import ¡sqrt ¡ pt1 ¡= ¡( 1.0, ¡5.0) ¡ pt2 ¡= ¡( 2.5, ¡1.5) ¡ ¡ ¡ line_length ¡= ¡sqrt((pt1[0] ¡-­‑ ¡pt2[0])**2 ¡+ ¡(pt1[1] ¡ -­‑ ¡pt2[1])**2) ¡ Source: ¡hAp://stackoverflow.com/ques6ons/2970608/what-­‑are-­‑named-­‑tuples-­‑in-­‑python ¡ 63

  10. Example ¡ ¡ ¡ ¡ ¡ line_length ¡= ¡sqrt((pt1[0] ¡-­‑ ¡pt2[0])**2 ¡+ ¡(pt1[1] ¡ -­‑ ¡pt2[1])**2) ¡ Source: ¡hAp://stackoverflow.com/ques6ons/2970608/what-­‑are-­‑named-­‑tuples-­‑in-­‑python ¡ 64

  11. Example from ¡collections ¡import ¡namedtuple ¡ from ¡math ¡import ¡sqrt ¡ Point ¡= ¡ namedtuple ('Point', ¡'x ¡y') ¡ pt1 ¡= ¡Point( 1.0, ¡5.0) ¡ pt2 ¡= ¡Point( 2.5, ¡1.5) ¡ ¡ line_length ¡= ¡sqrt((pt1.x ¡-­‑ ¡pt2.x)**2 ¡+ ¡(pt1.y ¡-­‑ ¡ pt2.y)**2) ¡ Source: ¡hAp://stackoverflow.com/ques6ons/2970608/what-­‑are-­‑named-­‑tuples-­‑in-­‑python ¡ 65

  12. It has cool methods Return ¡a ¡new ¡OrderedDict ¡which ¡ _asdict maps ¡field ¡names ¡to ¡their ¡values ¡ Class ¡method ¡that ¡makes ¡a ¡new ¡ _make(iterable) instance ¡from ¡an ¡exis6ng ¡sequence ¡ or ¡iterable. ¡ 66

  13. Extending NamedTuple from ¡collections ¡import ¡namedtuple ¡ ¡ _HotelBase ¡= ¡namedtuple( ¡ ¡ ¡ ¡ ¡ ¡ ¡'HotelDescriptor', ¡ ¡ ¡ ¡ ¡ ¡ ¡['cluster_id', ¡'trust_score', ¡'reviews_count', ¡ ¡ ¡'category_scores', ¡'intensity_factors'], ¡ ) ¡ ¡ class ¡HotelDescriptor(_HotelBase): ¡ ¡ ¡ ¡ ¡ def ¡compute_prior(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ¡not ¡self.trust_score ¡or ¡not ¡self.reviews_count: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ raise ¡NotEnoughDataForRanking("…") ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡_compute_prior(self.trust_score,…) ¡ 67

  14. 2. Tips & Tricks 2.1 ¡Iterators ¡& ¡Iterables ¡ 68

  15. l ¡= ¡[ 1, ¡2, ¡3, ¡4] ¡ for ¡i ¡in ¡x: ¡ ¡ print(x) ¡ 69

  16. a ¡generator ¡ ¡ expression ¡ is ¡ a ¡generator ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ is ¡ always ¡is ¡ iter() ¡ a ¡generator ¡ ¡ funcCon ¡ produces ¡ typically ¡is ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ (list, ¡dict, ¡etc) ¡ ¡ 70 By Vincent Driessen - Source: http://nvie.com/posts/iterators-vs-generators/

  17. an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ (an) ¡iterable ¡ 71

  18. an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ (an) ¡iterable ¡ comprehension ¡ 72

  19. an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ produces ¡ (an) ¡iterable ¡ comprehension ¡ 73

  20. an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ produces ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ (list, ¡dict, ¡etc) ¡ 74

  21. assert ¡1 ¡ in ¡[1, ¡2, ¡3] ¡ ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ produces ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ (list, ¡dict, ¡etc) ¡ 75

  22. assert ¡1 ¡ in ¡{1, ¡2, ¡3} ¡ ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ produces ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ (list, ¡dict, ¡etc) ¡ ¡ 76

  23. an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ produces ¡ typically ¡is ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ (list, ¡dict, ¡etc) ¡ ¡ 77

  24. l ¡= ¡[ 1, ¡2, ¡3, ¡4] ¡ x ¡= ¡iter(l) ¡ y ¡= ¡iter(l) ¡ ¡ ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ produces ¡ typically ¡is ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ (list, ¡dict, ¡etc) ¡ ¡ 78

  25. l ¡= ¡[ 1, ¡2, ¡3, ¡4] ¡ x ¡= ¡iter(l) ¡ y ¡= ¡iter(l) ¡ ¡ type(l) ¡ an ¡iterator ¡ >> ¡<class ¡'list'> ¡ next() ¡ Lazily ¡produce ¡ type(x) ¡ the ¡next ¡value ¡ >> ¡<class ¡'list_iterator'> ¡ always ¡is ¡ iter() ¡ produces ¡ typically ¡is ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ (list, ¡dict, ¡etc) ¡ ¡ 79

  26. l ¡= ¡[ 1, ¡2, ¡3, ¡4] ¡ x ¡= ¡iter(l) ¡ y ¡= ¡iter(l) ¡ ¡ type(l) ¡ an ¡iterator ¡ >> ¡<class ¡'list'> ¡ next() ¡ Lazily ¡produce ¡ type(x) ¡ the ¡next ¡value ¡ >> ¡<class ¡'list_iterator'> ¡ ¡ always ¡is ¡ next(x) ¡ iter() ¡ >> ¡ 1 ¡ next(y) ¡ produces ¡ typically ¡is ¡ >> ¡ 1 ¡ next(y) ¡ >> ¡ 2 ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ (list, ¡dict, ¡etc) ¡ ¡ 80

  27. l ¡= ¡[ 1, ¡2, ¡3, ¡4] ¡ for ¡e ¡ in ¡l: ¡ ¡ print (e) ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ produces ¡ typically ¡is ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ (list, ¡dict, ¡etc) ¡ ¡ 81

  28. Iterables ● An ¡ iterable ¡is ¡any ¡object ¡that ¡can ¡ return ¡an ¡ iterator ¡ ● Containers, ¡ ¡files, ¡sockets, ¡etc. ¡ ● Implement ¡ __iter__ (). ¡ ● Some ¡of ¡them ¡may ¡be ¡infinite ¡ ● The ¡ itertools ¡contain ¡many ¡ helper ¡func6ons ¡ 82

  29. class ¡InverseReader(object): ¡ ¡ ¡ ¡ ¡ def ¡__init__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ with ¡open('file.txt') ¡as ¡f: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.lines ¡= ¡f.readlines() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.index ¡= ¡len(self.lines) ¡-­‑ ¡ 1 ¡ ¡ ¡ ¡ ¡ ¡ def ¡__iter__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡self ¡ ¡ ¡ ¡ ¡ ¡ def ¡next(self): ¡ ¡# ¡Python ¡3 ¡__next__ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.index ¡-­‑= ¡ 1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ¡self.index ¡< ¡0: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ raise ¡StopIteration ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡self.lines[self.index] ¡ 83

  30. ir ¡= ¡InverseReader() ¡ ¡ for ¡line ¡in ¡ir: ¡ ¡ ¡ ¡ ¡ print ¡line ¡ 84

  31. a ¡generator ¡ ¡ expression ¡ is ¡ a ¡generator ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ is ¡ always ¡is ¡ always ¡is ¡ iter() ¡ a ¡generator ¡ ¡ funcCon ¡ produces ¡ typically ¡is ¡ (an) ¡iterable ¡ container ¡ comprehension ¡ 85

  32. a ¡generator ¡ ¡ expression ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ (an) ¡iterable ¡ 86

  33. a ¡generator ¡ ¡ expression ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ a ¡generator ¡ ¡ funcCon ¡ (an) ¡iterable ¡ 87

  34. a ¡generator ¡ ¡ expression ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ always ¡is ¡ iter() ¡ a ¡generator ¡ ¡ funcCon ¡ (an) ¡iterable ¡ 88

  35. a ¡generator ¡ ¡ expression ¡ is ¡ a ¡generator ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ is ¡ always ¡is ¡ iter() ¡ a ¡generator ¡ ¡ funcCon ¡ (an) ¡iterable ¡ 89

  36. a ¡generator ¡ ¡ expression ¡ is ¡ a ¡generator ¡ an ¡iterator ¡ next() ¡ Lazily ¡produce ¡ the ¡next ¡value ¡ is ¡ always ¡is ¡ always ¡is ¡ iter() ¡ a ¡generator ¡ ¡ funcCon ¡ (an) ¡iterable ¡ 90

  37. a ¡generator ¡ ¡ expression ¡ is ¡ a ¡generator ¡ is ¡ a ¡generator ¡ ¡ funcCon ¡ 91

  38. numbers ¡= ¡[x ¡ for ¡x ¡in ¡range(1, ¡10)] ¡ squares ¡= ¡[x ¡* ¡x ¡ for ¡x ¡in ¡numbers] ¡ a ¡generator ¡ ¡ expression ¡ type(squares) ¡ # ¡list ¡ is ¡ a ¡generator ¡ 92

  39. numbers ¡= ¡[x ¡ for ¡x ¡in ¡range(1, ¡10)] ¡ squares ¡= ¡[x ¡* ¡x ¡ for ¡x ¡in ¡numbers] ¡ a ¡generator ¡ ¡ expression ¡ type(squares) ¡ # ¡list ¡ is ¡ a ¡generator ¡ ¡ lazy_squares ¡= ¡(x ¡* ¡x ¡ for ¡x ¡in ¡numbers) ¡ lazy_squares ¡ # ¡<generator ¡object ¡<genexpr> ¡at ¡ 0x104c6da00> ¡ 93

  40. numbers ¡= ¡[x ¡ for ¡x ¡in ¡range(1, ¡10)] ¡ squares ¡= ¡[x ¡* ¡x ¡ for ¡x ¡in ¡numbers] ¡ a ¡generator ¡ ¡ expression ¡ type(squares) ¡ # ¡list ¡ is ¡ a ¡generator ¡ ¡ lazy_squares ¡= ¡(x ¡* ¡x ¡ for ¡x ¡in ¡numbers) ¡ lazy_squares ¡ # ¡<generator ¡object ¡<genexpr> ¡at ¡ 0x104c6da00> ¡ ¡ next(lazy_squares) ¡ # ¡1 ¡ next(lazy_squares) ¡ # ¡4 ¡ x ¡ 94

  41. numbers ¡= ¡[x ¡ for ¡x ¡in ¡range(1, ¡10)] ¡ squares ¡= ¡[x ¡* ¡x ¡ for ¡x ¡in ¡numbers] ¡ a ¡generator ¡ ¡ expression ¡ type(squares) ¡ # ¡list ¡ is ¡ a ¡generator ¡ ¡ lazy_squares ¡= ¡(x ¡* ¡x ¡ for ¡x ¡in ¡numbers) ¡ lazy_squares ¡ # ¡<generator ¡object ¡<genexpr> ¡at ¡ 0x104c6da00> ¡ ¡ next(lazy_squares) ¡ # ¡1 ¡ next(lazy_squares) ¡ # ¡4 ¡ ¡ lazy_squares ¡= ¡(x ¡* ¡x ¡ for ¡x ¡in ¡numbers) ¡ for ¡x ¡in ¡ lazy_squares: ¡ ¡ print ¡x ¡ ¡ 95 ¡

  42. a ¡generator ¡ is ¡ a ¡generator ¡ ¡ funcCon ¡ 96

  43. def ¡fib(): ¡ ¡ ¡prev, ¡curr ¡= ¡ 0,1 ¡ ¡ ¡ while ¡True: ¡ a ¡generator ¡ ¡ ¡ ¡ ¡ yield ¡curr ¡ ¡ ¡ ¡ ¡prev,curr ¡= ¡curr, ¡prev+curr ¡ ¡ ¡ is ¡ a ¡generator ¡ ¡ funcCon ¡ 97

  44. def ¡fib(): ¡ ¡ ¡prev, ¡curr ¡= ¡ 0,1 ¡ ¡ ¡ while ¡True: ¡ a ¡generator ¡ ¡ ¡ ¡ ¡ yield ¡curr ¡ ¡ ¡ ¡ ¡prev,curr ¡= ¡curr, ¡prev+curr ¡ ¡ f ¡= ¡fib() ¡ is ¡ ¡ a ¡generator ¡ ¡ next(f) ¡ funcCon ¡ # ¡1 ¡ next(f) ¡ # ¡1 ¡ next(f) ¡ # ¡2 ¡ ¡ 98

  45. def ¡fib(): ¡ ¡ ¡prev, ¡curr ¡= ¡ 0,1 ¡ ¡ ¡ while ¡True: ¡ a ¡generator ¡ ¡ ¡ ¡ ¡ yield ¡curr ¡ ¡ ¡ ¡ ¡prev,curr ¡= ¡curr, ¡prev+curr ¡ ¡ ¡ is ¡ for ¡x ¡in ¡islice(fib(), ¡0,3): ¡ ¡ ¡ ¡ ¡ print ¡x ¡ a ¡generator ¡ ¡ # 1 funcCon ¡ # 1 # 2 ¡ 99

  46. class ¡HdfsLineSentence(object): ¡ ¡ ¡ ¡ ¡ def ¡__init__(self, ¡source): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡self.source ¡= ¡source ¡ ¡ ¡ ¡ ¡ ¡ def ¡__iter__(self): ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡stream ¡= ¡self.source.open('r') ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ for ¡line ¡in ¡stream: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cid, ¡s ¡= ¡line.split(' \t') ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡# ¡decode ¡and ¡do ¡some ¡work ¡with ¡s ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ yield ¡s ¡ ¡ sentences ¡= ¡HdfsLineSentence(...) ¡ for ¡s ¡in ¡setences: ¡ ¡ ¡ ¡ ¡ print(s) ¡ 100

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend