-
Notifications
You must be signed in to change notification settings - Fork 48
/
financial_insights.py
36 lines (30 loc) · 1.01 KB
/
financial_insights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'''
Created on Apr, 2017
@author: hugo
'''
import numpy as np
def calc_ranks(x):
"""Given a list of items, return a list(in ndarray type) of ranks.
"""
n = len(x)
index = list(zip(*sorted(list(enumerate(x)), key=lambda d:d[1], reverse=True))[0])
rank = np.zeros(n)
rank[index] = range(1, n + 1)
return rank
def rank_bank_topic(bank_doc_map, doc_topic_dist):
"""Rank topics for banks
"""
bank_topic_ranks = {}
for each_bank in bank_doc_map:
rank = []
for each_doc in bank_doc_map[each_bank]:
rank.append(calc_ranks(doc_topic_dist[each_doc]))
rank = np.r_[rank]
# compute ranking score
bank_topic_ranks[each_bank] = np.mean(1. / rank, axis=0)
return bank_topic_ranks
if __name__ == '__main__':
n = 10
bank_doc_map = {'bank_0': ['doc_0', 'doc_1'], 'bank_1': ['doc_2', 'doc_3', 'doc_4']}
doc_topic_dist = dict([('doc_%s' % i, np.random.randn(n)) for i in range(5)])
rank = rank_bank_topic(bank_doc_map, doc_topic_dist)