【问题标题】:How to convert this code based on lists to code based on numpy arrays?如何将此基于列表的代码转换为基于numpy数组的代码?
【发布时间】: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)速度较慢,并且更难正确应用。
  • 使用rowdictionary 的样本以及列表版本的结果,我们或许可以提出改进建议。有时有一些方法可以用整个数组替换列表操作。但是复制列表追加不是其中之一。当您只有一个迭代循环(key)时,为什么要附加到 2 个列表?
  • @hpaulj 你的意思是这两行temp_list.append(dictionary[key]) output_sentence.append(temp_list)吗?

标签: python arrays python-3.x numpy keras


【解决方案1】:
output_sentence = []
for key in row['o'].lower():
    if key not in dictionary.keys():
        dictionary[key] = len(dictionary)
    output_sentence.append(dictionary[key])

final_output_sentence = np.array(output_sentence).reshape(1,-1,1)
  • 如果字典中不存在key,则使用下一个大小添加它
  • 将key对应的值追加到output_sentence
  • 最后,output_sentence 是一个列表,但由于您想要一个 3D 数组,请将其转换为 numpy 数组并对其进行整形。
  • x.reshape(1,-1,1) => 重塑 x 使得第 0 轴的大小为 1,第 2 轴的大小为 1,第 1 轴的大小将与 x 中的元素相同。

【讨论】:

  • @NinoFiliu Done 将确保从现在开始这样做。
猜你喜欢
  • 2022-06-20
  • 1970-01-01
  • 2021-06-14
  • 2018-12-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2015-06-10
  • 2023-04-11
相关资源
最近更新 更多