/
SeregKa
/
python-topic-model
Обзор
Документация
Войти
/
SeregKa
/
python-topic-model
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
ptm/utils.py
124 строки
3 KB
Dongwoo Kim
Add example of author-topic model with cora dataset #4
15 фев 2016, 01:03
15 фев 2016, 01:03
74de3de
Код
Авторство
О чём код?
import numpy as np from six.moves import xrange def sampling_from_dist(prob): """ Sample index from a list of unnormalised probability distribution same as np.random.multinomial(1, prob/np.sum(prob)).argmax() Parameters ---------- prob: ndarray array of unnormalised probability distribution Returns ------- new_topic: return a sampled index """ thr = prob.sum() * np.random.rand() new_topic = 0 tmp = prob[new_topic] while tmp < thr: new_topic += 1 tmp += prob[new_topic] return new_topic def sampling_from_dict(prob): """ sample key from dictionary `prob` where values are unnormalised probability distribution Parameters ---------- prob: dict key = topic value = unnormalised probability of the topic Returns ------- key: int sampled key """ prob_sum = sum(prob.values()) thr = prob_sum * np.random.rand() tmp = 0 for key, p in prob.items(): tmp += p if tmp < thr: new_topic = key return new_topic def isfloat(value): """ Check the value is convertable to float value """ try: float(value) return True except ValueError: return False def read_voca(path): """ open file from path and read each line to return the word list """ with open(path, 'r') as f: return [word.strip() for word in f.readlines()] def word_cnt_to_bow_list(word_ids, word_cnt): corpus_list = list() for di in xrange(len(word_ids)): doc_list = list() for wi in xrange(len(word_ids[di])): word = word_ids[di][wi] for c in xrange(word_cnt[di][wi]): doc_list.append(word) corpus_list.append(doc_list) return corpus_list def log_normalize(log_prob_vector): """ returns a probability vector of log probability vector """ max_v = log_prob_vector.max() log_prob_vector += max_v log_prob_vector = np.exp(log_prob_vector) log_prob_vector /= log_prob_vector.sum() return log_prob_vector def convert_cnt_to_list(word_ids, word_cnt): corpus = list() for di in xrange(len(word_ids)): doc = list() doc_ids = word_ids[di] doc_cnt = word_cnt[di] for wi in xrange(len(doc_ids)): word_id = doc_ids[wi] for si in xrange(doc_cnt[wi]): doc.append(word_id) corpus.append(doc) return corpus def write_top_words(topic_word_matrix, vocab, filepath, n_words=20, delimiter=',', newline='\n'): with open(filepath, 'w') as f: for ti in xrange(topic_word_matrix.shape[0]): top_words = vocab[topic_word_matrix[ti, :].argsort()[::-1][:n_words]] f.write('%d' % (ti)) for word in top_words: f.write(delimiter + word) f.write(newline) def get_top_words(topic_word_matrix, vocab, topic, n_words=20): if not isinstance(vocab, np.ndarray): vocab = np.array(vocab) top_words = vocab[topic_word_matrix[topic].argsort()[::-1][:n_words]] return top_words