python - numpy使用中的一个错误

 263企业邮箱温州授权合作伙伴 发布于 2022-10-29 23:13
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里面呈现?

2 个回答
  • while cishu > 1:
    这里有2个步骤

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

    cishu > 1 这里计算出的结果是一个numpy.ndarray,没法做boolean计算的

    2022-10-31 18:54 回答
  • x是列表,cishu=x,cishu > 1是什么意思呢?一个列表大于1?

    2022-10-31 18:55 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有