【问题标题】:How to loop through multiple numpy arrays and append the item from one array to another with same id?如何遍历多个numpy数组并将项目从一个数组附加到另​​一个具有相同ID的数组?
【发布时间】:2018-05-10 11:21:30
【问题描述】:

如果我有以下数据,test_df['review_id'],其中包含数据框的 id。我需要将它们中的每一个与其他数组中的数据配对。我将有如下代码。

def classify_nb_report(X_train_vectorized, y_train, X_test_vectorized, y_test):
    clf = MultinomialNB()

    # TRAIN THE CLASSIFIER WITH AVAILABLE TRAINING DATA
    clf.fit(X_train_vectorized, y_train)

    y_pred_class = clf.predict(X_test_vectorized)

    return y_pred_class

for i in range(0, n_loop):
    train_df, test_df = train_test_split(df, test_size=0.3)
    ....
    nb_y = classify_nb_report(X_train_vectorized, y_train, X_test_vectorized, y_test)

正如您在上面看到的,在每次迭代中,我都会得到一组新的nb_y,它是一个 numpy 数组。我还将拥有不同的 test_dftrain_df 集(由上面的函数随机选择)。我想将每次迭代中 nb_y 的每个值与匹配 test_df['review_id']id 配对。

通过下面的代码,我可以得到test_df 的id 和nb_y 的值。

for f, b in zip(test_df['review_id'], nb_y):
    print(f, b)

结果:

17377 5.0
18505 5.0
24825 1.0
16032 5.0
23721 1.0
18008 5.0

现在,我想要的是,根据上面的结果,我将下一次迭代中 nb_y 的值附加到它们对应的 id 中。

我希望这不会太令人困惑,如果我的问题不够清楚,我会尝试扩展更多。提前致谢。

【问题讨论】:

  • 我认为您可以使用列表字典来解决您的问题。键是 id,列表将包括所有 nb_y 值。我不确定这是否是您想要的,或者我所说的是否清楚。如果需要,我可以稍后写一个详细的答案。
  • @MattSt 是的,我也在考虑使用字典。但是在每次迭代中,总会有新值从nb_y 添加到相应的 id。而且我不确定如何在每个循环中修改字典。
  • 如果 id 已经在 dictionary.keys() 中,你应该附加到字典中。否则,您应该添加一个包含第一个 nb_y 元素的列表(例如,dictionary[id] = [nb_y])。我可以在答案中为您编写代码,但这是您想要的吗?我不清楚。
  • @MattSt 我在test_df['review_id'] 中有ID,正如我在原帖中提到的那样。没问题,把代码贴出来,我会看到的。

标签: python arrays numpy


【解决方案1】:

在参考了thisthis之后,我终于想出了自己的解决方案。我把上面的代码变成了这样。

def classify_nb_report(X_train_vectorized, y_train, X_test_vectorized, y_test):
    clf = MultinomialNB()

    # TRAIN THE CLASSIFIER WITH AVAILABLE TRAINING DATA
    clf.fit(X_train_vectorized, y_train)

    y_pred_class = clf.predict(X_test_vectorized)

    return y_pred_class


nb_y_list = []

for i in range(0, n_loop):
    train_df, test_df = train_test_split(df, test_size=0.3)
    ....
    nb_y = classify_nb_report(X_train_vectorized, y_train, X_test_vectorized, y_test)

    nb_y_list.extend([list(x) for x in zip(test_df['review_id'],nb_y)])

dd = defaultdict(list)
for key, val in nb_y_list:
     dd[key].append(val)
     print(dd)

基本上,我首先创建了一个名为nb_y_list 的空列表。然后对于每次迭代,我将zip 来自test_df['review_id'] 的id 与来自nb_y 的值平行,并将它们扩展到之前的nb_y_list。完成所有循环后,我将获得完整列表,现在我需要使用 defaultdict() 将其转换为字典。

【讨论】:

    【解决方案2】:

    我不确定我是否正确理解了问题以及您的其余代码如何工作,但我认为以下代码可能会满足您的需求。让我知道它是否有效或答案是否有问题。

    dictionary = {}
    for i in range(0, n_loop):
        train_df, test_df = train_test_split(df, test_size=0.3)
        ....
        nb_y = classify_nb_report(X_train_vectorized, y_train, X_test_vectorized, y_test)
        id = test_df['review_id']
        if not id in dictionary.keys():
            dictionary[id] = [nb_y]
        else:
            dictionary[id].append(nb_y)
    

    【讨论】:

    • 我尝试了您的代码,但我不确定空的 dictionary 将如何工作。谢谢你的努力,我真的很感激。我在上面提出了自己的解决方案。
    • 当您编写 dictionary[id] = [nb_y] 时,将初始化具有键 id 的字典元素。您不必从头开始为字典初始化键(至少在 python 3.6.4 中)。我刚刚编辑的 if 语句中有一个错误。
    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 2015-09-12
    • 1970-01-01
    • 2015-09-19
    • 2018-04-07
    • 2017-07-22
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多