0
我需要幫助減少下面的代碼的圈複雜度:的Python:降低圈複雜度
def avg_title_vec(record, lookup):
avg_vec = []
word_vectors = []
for tag in record['all_titles']:
titles = clean_token(tag).split()
for word in titles:
if word in lookup.value:
word_vectors.append(lookup.value[word])
if len(word_vectors):
avg_vec = [
float(val) for val in numpy.mean(
numpy.array(word_vectors),
axis=0)]
output = (record['id'],
','.join([str(a) for a in avg_vec]))
return output
例輸入:
record ={'all_titles': ['hello world', 'hi world', 'bye world']}
lookup.value = {'hello': [0.1, 0.2], 'world': [0.2, 0.3], 'bye': [0.9, -0.1]}
def clean_token(input_string):
return input_string.replace("-", " ").replace("/", " ").replace(
":", " ").replace(",", " ").replace(";", " ").replace(
".", " ").replace("(", " ").replace(")", " ").lower()
所以一切都存在於lookup.value的話,我正在考慮它們的矢量形式的平均值。
你介意解釋代碼試圖在第一個地方做什麼? –
增加了一些更多的細節 – futurenext110
我試着從一開始就編碼這個自己,我結束了相同的代碼:) –