代码之家  ›  专栏  ›  技术社区  ›  artemis Roberto

意外的python类型错误:使用scalars时

  •  1
  • artemis Roberto  · 技术社区  · 5 年前

    我对Python是新手,在我看来,它与Java有很大的不同。

    我看过其他的答案,这意味着错误是因为当数组期望值时,我正在传递一个数组。我不知道。我很确定我只是在传递一个值。

    第97行是: exponential = math.exp(-(math.pow(feature_value-mean, 2) / (2*math.pow(standard_deviation, 2))))

    错误的完整文本是:

    Traceback (most recent call last):
      File "D:/Personal/Python/NB.py", line 153, in <module>
        main()
      File "D:/Personal/Python/NB.py", line 148, in main
        predictions = getPredict(summaries, testing_set)
      File "D:/Personal/Python/NB.py", line 129, in getPredict
        classification = predict(results, testData[index])
      File "D:/Personal/Python/NB.py", line 117, in predict
        probabilities = Classify(feature_summaries, classifications)
      File "D:/Personal/Python/NB.py", line 113, in Classify
        probabilities[classes] = probabilities[classes] * GaussianProbabilityDensity(feature_value, mean, standard_deviation)
      File "D:/Personal/Python/NB.py", line 97, in GaussianProbabilityDensity
        exponential = math.exp(-(math.pow(feature_value-mean, 2) / (2*math.pow(standard_deviation, 2))))
    TypeError: only size-1 arrays can be converted to Python scalars
    

    如果有用的话, csv 在下面。应该注意的是,我还有另外两个算法可以在这个数据集上运行。

    | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
    | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 0 | 0 |
    | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 |
    | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
    | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 0 | 0 | 0 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 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
    | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
    | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 |
    | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 1 |
    | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 |
    | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
    | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
    | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 |
    | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   hpaulj    5 年前

    math.pow (就像所有人一样) math 函数)只适用于标量,即单个数字(整数或浮点)。错误表明其中一个参数,例如 standard_deviation 是一个 numpy 具有多个元素的数组,因此无法将其转换为标量并传递给 马普 .

    这发生在您自己的代码中,所以跟踪这些变量回到它们的源代码中没有困难。

    您可能无意中向此函数传递了一个数组,或者需要替换 马普 具有 np.pow (和) np.exp )用于数组的函数。


    你生成一个 麻木的 从中加载时的数组 csv

    data = numpy.loadtxt(data, delimiter=',')
    
    # Loop through the data in the array
    for index in range(len(data)):
        # Utilize a try catch to try and convert to float, if it can't convert to float, converts to 0
        try:
            data[index] = [float(x) for x in data[index]]
        except ValueError:
            data[index] = 0
    

    loadtxt 返回具有float dtype(默认值)的数组。它的所有元素都将是浮动的-如果它读取的内容不是有效的浮动,它将引发一个错误。因此不需要循环。循环看起来太像是为列表而写的,而不是数组。

    randomize_data 不应该归还任何东西。 np.random.shuffle 操作到位 猪瘟病毒 . 这不会导致错误。

        2
  •  2
  •   Grismar    5 年前

    你的问题是 class_summaries (从第107行)是元组列表,您可以选择其中一个元组并将其传递到 GaussianProbabilityDensity 函数作为 feature_value .

    它最终导致了第97行的错误。请注意,如果要修复它(我用一个硬1.0替换了该值),最终会得到一个除数为零的错误,如 standard_deviation 你的输入恰好是0。

    我发现这一点的方法是使用具有适当调试器(我喜欢pycharm)的python ide,并在指定的行上设置断点,在错误发生之前检查各种变量。我建议尝试以类似的方式解决这些类型的问题,因为这样可以节省大量的时间和虚假的 print 声明。