cari

Rumah  >  Soal Jawab  >  teks badan

python - numpy使用中的一个错误

import numpy as np
import matplotlib.pyplot as plt
from math import pow
from math import e

def main(x):
    dishu = e
    zhishu = 1/e
    result = zhishu
    cishu = x
    while cishu > 1:
        result = pow(zhishu, result)
        cishu -= 1
    main_result = pow(dishu, result)
    return main_result
    

x = np.linspace(2, 100, 90)
y = main(x)

这段代码在运行时抛出的一段错误是:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我在stackoverflow上面也搜过同样的错误信息,但我不明白是这里为什么有多种返回的可能性,这个问题究竟该如何解决?

我试着用for去一个个的得到结果,但这回到了matplotlib报错:

ValueError: x and y must have same first dimension

请问该怎样写才能将main这个函数的图像在matplotlib里面呈现?

阿神阿神2889 hari yang lalu607

membalas semua(2)saya akan balas

  • 阿神

    阿神2017-04-17 17:35:52

    x ialah senarai, cishu=x, cishu > Senarai lebih daripada 1?

    balas
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:35:52

    sementara cishu > 1:
    Ada 2 langkah di sini

    1) 先计算 cishu > 2,得到一个临时变量
    2)  对于1)得到的临时变量, 用while计算其boolean值,为False时返回
    

    cishu > 1 Hasil yang dikira di sini ialah numpy.ndarray, yang tidak boleh dikira dengan boolean

    balas
    0
  • Batalbalas