【发布时间】:2019-03-23 21:15:34
【问题描述】:
我正在使用 keras,并且总是有一个带有列表的 from,所以我想总是所有东西都必须转换为 numpy 数组,这对我来说是非常不合逻辑的。我猜它与性能有关?我看不出还有什么原因?但是我的问题如下所示。我必须转换这部分代码:
output_sentence = []
final_output_sentence = []
for key in row['o'].lower():
temp_list = []
if key in dictionary.keys():
temp_list.append(dictionary[key])
output_sentence.append(temp_list)
else:
dictionary[key] = len(dictionary)
temp_list.append(dictionary[key])
output_sentence.append(temp_list)
final_output_sentence.append(output_sentence)
基于 numpy 数组的代码。我是这样尝试的:
output_sentence = np.array([], dtype=int)
final_output_sentence = np.array([], dtype=int)
for key in row['o'].lower():
temp_list = np.array([], dtype=int)
if key in dictionary.keys():
temp_list = np.append(temp_list, dictionary[key])
output_sentence = np.append(output_sentence, temp_list)
else:
dictionary[key] = len(dictionary)
temp_list = np.append(temp_list, dictionary[key])
output_sentence = np.append(output_sentence, temp_list)
final_output_sentence = np.append(final_output_sentence, output_sentence)
但是,我得到的不是这个[[[1], [2], [3], [2], [4]]],而是这个[1 2 3 2 4]。任何想法如何解决这个问题?
更新
您对下面显示的解决方案有何看法?性能优化有什么技巧吗?
output_sentence = []
for key in row['o'].lower():
temp_list = []
if key in dictionary.keys():
temp_list.append(dictionary[key])
output_sentence.append(temp_list)
else:
dictionary[key] = len(dictionary)
temp_list.append(dictionary[key])
output_sentence.append(temp_list)
final_output_sentence = np.array(output_sentence)
final_output_sentence = final_output_sentence.reshape(1, final_output_sentence.shape[0], 1)
【问题讨论】:
-
一般情况下最好使用列表和列表
append,最后创建一个调用的数组。np.append(和其他版本的np.concatenate)速度较慢,并且更难正确应用。 -
使用
row和dictionary的样本以及列表版本的结果,我们或许可以提出改进建议。有时有一些方法可以用整个数组替换列表操作。但是复制列表追加不是其中之一。当您只有一个迭代循环(key)时,为什么要附加到 2 个列表? -
@hpaulj 你的意思是这两行
temp_list.append(dictionary[key]) output_sentence.append(temp_list)吗?
标签: python arrays python-3.x numpy keras