728x90
728x90
728x90

Python built-in인 islice을 활용한다.

 

from itertools import islice

for ind, row in islice(df.itertuples(index=False), 1, None):  # 2번째 row부터 iteration 시작

 

728x90

'ML' 카테고리의 다른 글

[Pandas]중복인것만 살리기  (0) 2020.10.25
[Pandas]itertuples에 관하여  (0) 2020.10.25
contextual data, contextual features, context features  (0) 2020.10.19
(미완)Python 3.9의 주요 특징  (0) 2020.10.06
Streaming or stateful metric  (0) 2020.10.06
728x90

-d ->detached mode

-p port1:port2 -> port1으로 받으면 container 내에 port2 service로 접속할 수 있게 설정

-e "STH" -> environment variable 설정

-v: host_dir:container_dir -> host_dir를 container_dir로 mount

-c "CMDs" -> docker 내부에서 commands를 실행하면서 켜짐

 

728x90

'CS' 카테고리의 다른 글

(미완)[Elasticsearch]특징  (0) 2020.10.29
Inverted index 이해하기  (0) 2020.10.29
(미완)Ubuntu 명령어 정리  (0) 2020.10.21
[Numpy] np.vectorize는 사용하지 말자.  (0) 2020.10.20
[Numpy]np.select  (0) 2020.10.19
728x90

mkdir:make directory

  -p:parent 경로가 필요하다면 같이 만듦

 

tree:현재 경로 내 하위 폴더들의 tree 구조를 보여줌

 

728x90

'CS' 카테고리의 다른 글

Inverted index 이해하기  (0) 2020.10.29
(미완)[Docker] option 정리  (0) 2020.10.21
[Numpy] np.vectorize는 사용하지 말자.  (0) 2020.10.20
[Numpy]np.select  (0) 2020.10.19
[Algorithm]백준, 14888, 연산자 끼워넣기  (0) 2020.10.16
728x90

numpy는 기본적으로 array-wise하게 계산할 때 효율성을 얻는다.

그것을 위해서 다음과 같은 method를 활용하고자 하는데

 

def aux(x):

  if x:

    return x*np.log(x)

  else:

    return 0

func = np.vectorize(aux)

func(ndarray)

이 때, 사실 array-wise하게 계산하지 않는다.

따라서 large ndarray에 대해서 계산이 느리다.

 

이 때, 다음과 같이 aux를 수정하고 aux를 그대로 사용하자.

def aux(x):

  return np.where(x > 0, x*np.log(x), 0)

aux(ndarray)

 

 

 

# 하 코테에서 조금만 시간있었으면 해결헀을 텐데, 너무나 짜증난다.

 

728x90

'CS' 카테고리의 다른 글

(미완)[Docker] option 정리  (0) 2020.10.21
(미완)Ubuntu 명령어 정리  (0) 2020.10.21
[Numpy]np.select  (0) 2020.10.19
[Algorithm]백준, 14888, 연산자 끼워넣기  (0) 2020.10.16
[Algorithm]백준, 10819, 차이를 최대로  (0) 2020.10.16
728x90

장점:

-domain knowledge가 필요 없음, embedding 알아서 학습하므로

-serendipity, 즉 해당 유저의 기존 interest 밖의 상품도 추천이 가능

-No need any contextual features, just the feedback matrix, 따라서 여러 추천 엔진 중 바로 적용하기 좋다.

-folding 문제를 해결(objective function에서 unobserved data에 대한 weight를 조절하여) 가능

-user/item embedding을 미리 계산해두고 저장하여 사용함으로써 Serving이 편리

-아주 많은 items가 존재하여도, user/item matrix가 대개 sparse하므로 scalability가 있음

 

단점:

-cold-start problem, 즉 training data에 한번도 없었던 new item은 추천되지 않는다.

  -해결방안 1, 만약 new item이 training data엔 없었지만 신규 거래내역/평점내역 등이 많이 존재한다면 전체를 재학습시켜도 되지만, 해당 new item만 embedding vector를 쉽게 학습시킬 수 있다.(projection in WALS) 즉, User embedding 그대로 유지시키고 new item vector만 학습

  -해결방안 2, new item과 유사한 items(유튜브라면 같은 업로더가 올린 타 동영상들, commerce라면 같은 분류나 같은 브랜드 내의 상품들 등)의 embedding vectors를 평균을 취하는 방식으로 new item embedding vector를 approximate

-User/Item의 feature를 함께 고려하기가 어렵다.

  -해결방안, 기존 user-item matrix에 row로는 item feature을, column으로는 user feature를 나타내는 multi-hot encoding block matrix를 추가하여 학습하면, user/item/each feature embedding을 모두 얻을 수가 있다.

 

 

728x90
728x90

Contextual data is data that gives context to a person, entity or event. It is commonly used by universities in order to determine academic potential of applicants as well as business organizations for market research and prediction. Contextual data is taken from various sources and may include family and socioeconomic background, educational history, health background, general environment and many other factors.

 

즉, 추천 시스템에서는 한 고객을 구매내역으로만 특징 짓다가 다른 요소를 고려하여 반영할 수 있다. 이러한 다른 요소(contextual features)로는 고객의 성별, 주거지 등이 될 수 있다.

 

혹은 고객에 대한 정보 뿐 아니라 고객-상품 과 관련없이 "날씨" 등도 고려할 수 있으며 이것 또한 contextual feature가 되겠다.

728x90
728x90

SGD(Stochastic gradient descent)

WALS(Weighted alternating least squares)

 

WALS란 어떤 방식이냐,

Fixing U and solving for V

Fixing V and solving for U

각 과정을 교차(alternating)하면서 objective function내에 square term을 최소화

각 과정은 linear system이므로 exactly하게 solved됨

objective function을 보면, Fixing U에선 V에 대한 convex, Fixing V에선 U에 대한 convex function이다. 다만, U와 V에 동시에 살피면 convex가 아니다. 즉 not jointly convex

 

SGD vs WALS

 

SGD의 장단점:

-(장점)flexible, loss가 꼭 error square형태가 아니어도 가능

-(장점)병렬계산처리 가능

-(단점)수렴 속도가 WLAS보단 느림

-(단점)unobserved data 다루기가 힘듦, negative sampling이나 gravity 사용이 필요

 

WLAS의 장점:

-(장점)병렬계산처리 가능

-(장점)SGD보다 빠르게 수렴

-(장점)unobserved data 다루기가 쉬움

-(단점)error square형태의 loss에 대해서만 가능

 

 

 

728x90
728x90

A(User-item matrix)에서 (entry는 feedback)

 

Observed only MF는 오직 observed data에 대해서만 objective function에 포함

Weighted MF는 unobserved data 또한 objective function에 포함 with some weight

SVD는 Observed+Unobserved, 즉 모두 포함(따라서 Frobenius norm으로 계산)

 

Observed only MF는 loss가 낮아보이겠으나, 대다수가 0이 아닌 값으로 학습하여(특히 A가 binary 1/0라면 모두 1로 학습해버리는 경향을 띔) generalization 능력이 떨어짐

SVD는 User-item matrix가 굉장히 sparse하여 대다수가 0으로 학습하여 generalization 능력이 떨어짐

Weighted MF는 unoserved data에 적당한 weight를 주어 0과 1에 치우치지 않게 학습(observed data에서도 training set에 많이 등장한 (u,i)는 가중치를 더 줄 수 있음)

 

728x90
728x90

장점:

-타 유저의 데이터를 필요로 하지않아 easy to scale to a large number of users

-해당 유저 고유의 interest를 capture하기 때문에, 덕후들만이 좋아할(대다수 유저들은 좋아하지 않는) 상품이 추천 가능

 

단점:

-item embedding에 있어서 domain knowledge의 수준에 따라 모델 성능 좌우

-유저의 interest 내에서만 추천이 돌기 때문에, interest를 확장할 수는 없다.

728x90
728x90

cosine VS dot product

-dot product가 cosine에 비해 sensitive to the norm of the embedding.

-training set에서 자주 등장한 상품은 norm이 큰 경향이 있음

-따라서 "popularity"를 고려한다면 dot product를 쓰자.

-하지만, popular items가 recommendation을 dominate한다면,

s(i,j) = ||i||^α ||j||^α cos(i,j)

형태로 norm에 대한 일련의 조정을 하여 사용하자.

 

그리고 주의할 점,

training set에 아주 적게 등장한 상품은 embedding이 update가 적게 될 것이다.

근데, 만약 initial embedding의 norm이 아주 큰 상황에서

dot product로 similarity를 measure하여 사용한다면

아주 rare하게 등장한 상품이 recommendation을 dominate할 수도 있음

따라서, initializer를 적절하게 택하는게 필요

 

 

728x90

+ Recent posts