A.I
MapReduce 원리 본문
맵리듀스(Mapreduce)¶
- in_key = Split의 결과로 생긴 partitioning 키값. 키값은 최종 Output에는 반영되지 않음
- map함수는 Split된 데이터를 가져다가 out_key와 intermediate_value의 리스트로 변환. out_key는 map함수가 결과물을 구분하는 기준 키값.
- reduce 함수의 입력은 여러 map함수의 intermediate_value들을 out_key별로 구분해서 리스트업한 것을 입력으로 함. 이것을 가지고 reduce 함수는 out_key 기준으로 sum을 할 것입니다. 그래서 reduce 함수sms 최종적으로 out_value의 list를 출력합니다.
파이썬 함수의 특징¶
함수는 다른 함수의 인자로 전달될 수 있다.¶
In [1]:
#에러메세지를 출력하는 함수
def errormessage(format_func, msg):
print(format_func(msg))
#단어의 첫글자만 대문자로 만드는 함수
def to_upper(msg):
t = msg.split(" ")
T = []
for i in t:
i = i[0].upper() + i[1:]
T.append(i)
T = " ".join(T)
return T
print("슝=3")
슝=3
In [2]:
msg = "you have limited access"
errormessage(to_upper, msg)
You Have Limited Access
함수는 다른 함수의 인자로 전달될 수 있다¶
In [3]:
def france():
print('bonjour')
def italy():
print('ciao')
print("슝=3")
슝=3
In [4]:
hello = france
print(hello)
<function france at 0x7f87d056e3b0>
In [5]:
hello()
bonjour
In [6]:
print(type(hello))
<class 'function'>
함수는 다른 함수의 결과로서 반환될 수 있다.¶
In [7]:
def func1(x):
return x**2
def func2():
return func1
print("슝=3")
슝=3
In [8]:
print(func1(3))
print(func2()(3))
9 9
함수형 프로그래밍과 맵리듀스¶
In [9]:
# List, Tuple, Dictionary, Set의 자료형을 컬렉션이라고 함.
import collections
print("슝=3")
슝=3
In [15]:
from collections import defaultdict
text = """I have a depression, and then there was a girl who came into
my life. One day, my life was changed because that girl just changed my
life. She taught me how to love and how to be an active person.
And then, I feel so happy when I am always with her.
I love her so much. I don't want let her go. I am sad because she
is with her favorite friends. I will do anything to make her proud"""
result = defaultdict(int)
word = text.split( )
for i in word:
result[i] +=1
print(result['girl'])
2
In [16]:
# mapping
mynum = ['1','2','3','4']
mynum_int = list(map(int, mynum)) # mynum의 각 원소에 int 함수를 적용
print(mynum_int)
mynum_square = list(map(lambda x : x*x, mynum_int)) # mynum_int의 각 원소 x에 lambda x : x*x 함수를 적용
print(mynum_square)
[1, 2, 3, 4] [1, 4, 9, 16]
In [19]:
# filtering
mynum = range(-5, 4)
mynum_plus = list(filter(lambda x: x > 0, mynum)) # mynum의 각 원소 x에 대해 lambda x: x > 0 인지 체크하는 필터를 적용
print(mynum_plus)
[1, 2, 3]
In [20]:
# reducing
from functools import reduce
mynum = [1, 2, 3, 4, 5]
add = reduce((lambda x, y: x + y), mynum) # reduce는 내부에 관리하는 x 변수에 mynum의 각 원소 y를 차례차례 더하여 x에 반영한다.
print(add)
15
- 맵리듀스는 크게 4가지 순서로 나뉘며 크게, map task(map, split)와 reduce task(shuffle, reduce) 로 나뉜다
맵리듀스로 알파벳 수 찾기¶
- 입력 데이터 - hello python
- Split - h,e,l,l,o,(공백),p,y,t,h,o,n
- Map - (h:1, e:1, l:1, l:1, o:1, ' ':1, …)
- Reduce - (h:1, e:1, l:2…)
In [21]:
#input data
text = 'hello python'
# 텍스트 스플릿 내서 list에 저장
def mapper(text):
split = []
for i in text:
split.append((i, 1))
return split
print("슝=3")
슝=3
In [22]:
# mapper에서 받은 값 중 같은 값은 더한다
def reducer(split):
out = {}
for i in split:
if i[0] not in out.keys():
out[i[0]] = 1
else:
out[i[0]] += 1
return out
print("슝=3")
슝=3
In [23]:
reducer(mapper(text))
Out[23]:
{'h': 2, 'e': 1, 'l': 2, 'o': 2, ' ': 1, 'p': 1, 'y': 1, 't': 1, 'n': 1}
'파이썬 & AI 학습' 카테고리의 다른 글
Hadoop & Spark (0) | 2021.03.10 |
---|---|
Linear, Convolution layer (0) | 2021.03.06 |
TensorFlow v2 다뤄보기 (0) | 2021.02.26 |
파이썬으로 DB 다루기 (0) | 2021.02.24 |
딥러닝에 대한 개념 학습 (0) | 2021.02.21 |