【问题标题】:TypeError: only size-1 arrays can be converted to Python scalarsTypeError:只有大小为 1 的数组可以转换为 Python 标量
【发布时间】:2018-07-22 18:34:21
【问题描述】:

我有一个由字符串组成的 (61000L, 2L) numpy.ndarray。 如中,numpy.ndarray 中的项目是字符串。

我拆分字符串,以便将字符串中的每个单词作为列表输出,在 numpy.ndarray 中,代码如下:

words_data = np.char.split(string_data)

我尝试创建一个双 for 循环来计算在每个列表中找到的唯一单词。

from collections import Counter
counts = Counter()
for i in range(words_data.shape[0]):
    for j in range(words_data[1]):
        counts.update(words_data[i])

counts

上面代码的输出错误如下:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-680a0105eebd> in <module>()
      1 counts = Counter()
      2 for i in range(words_data.shape[0]):
----> 3     for j in range(words_data[1]):
      4         counts.update(words_data[i])
      5 

TypeError: only size-1 arrays can be converted to Python scalar

这是我数据的前 8 行:

 x = np.array([["hello my name is nick", "hello my name is Nick", "hello my name is Carly", "hello my name is Ashley, "hello my name is Java", "hello my name is C++", "hello my name is Ruby", "hello my name is Python"" ],["hello my name is Java", "hello my name is C++", "hello my name is Ruby", "hello my name is Python", "hello my name is nick", "hello my name is Nick", "hello my name is Carly", "hello my name is Ashley]])

 x =  x.transpose()

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    这里不需要循环。这是一种解决方案:

    from collections import Counter
    from itertools import chain
    import numpy as np
    
    string_data = np.array([["hello my name is nick", "hello my name is Nick", "hello my name is Carly",
                             "hello my name is Ashley", "hello my name is Java", "hello my name is C++",
                             "hello my name is Ruby", "hello my name is Python"],
                             ["hello my name is Java", "hello my name is C++", "hello my name is Ruby",
                              "hello my name is Python", "hello my name is nick", "hello my name is Nick",
                              "hello my name is Carly", "hello my name is Ashley"]])
    
    word_count = Counter(' '.join(chain.from_iterable(string_data)).split())
    
    # Counter({'Ashley': 2,
    #          'C++': 2,
    #          'Carly': 2,
    #          'Java': 2,
    #          'Nick': 2,
    #          'Python': 2,
    #          'Ruby': 2,
    #          'hello': 16,
    #          'is': 16,
    #          'my': 16,
    #          'name': 16,
    #          'nick': 2})
    

    【讨论】:

    • 当我运行上面的代码时,它给了我一个TypeError: unhashable type: 'list'@jp_data_analysis
    • @JayganeshKalla,在 python 3.6 / numpy 1.11 上为我工作。你在测试我发布的内容吗?
    • 我在单独的 jupyter 笔记本上尝试了您的代码并且它有效,但是当我尝试使用我的数据时,它会输出我提到的错误。我正在使用 Python 2.7.14/numpy 1.14.0,@jp_data_analysis
    • 在这种情况下,您必须展示您的数据样本,以便我们有一个可重复的示例。
    • 我已经添加了前 8 行
    猜你喜欢
    • 2021-10-17
    • 2021-06-24
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2019-06-19
    • 2020-08-09
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多