gpt4 book ai didi

python - 使用 Numpy 查找输入数字集的均值、中值、众数或范围

转载 作者:太空狗 更新时间:2023-10-29 21:29:19 25 4
gpt4 key购买 nike

我正在创建一个程序来查找均值、中值、众数或范围。当我运行它时,它工作正常,直到它到达计算答案的部分。它给了我一个“无法使用灵活类型进行预成型”错误。我搜索了此错误,但找不到我需要修复的内容。这是我第一次使用 numpy,所以任何帮助都会很棒。

import sys
import numpy as np

welcomeString = input("Welcome to MMMR Calculator\nWhat would you like to calculate(Mean,Median,Mode,Range):")

if welcomeString.lower() == "mean":
meanNumbers = input("What numbers would you like to use?:")
print (np.average(meanNumbers))
stop = input()

if welcomeString.lower() == "median":
medianNumbers = input("What numbers would like to use?:")
print (np.median(medianNumbers))
stop = input()

if welcomeString.lower() == "mode":
modeNumbers = input("What numbers would you like to use?:")
print (np.mode(modeNumbers))
stop = input()

if welcomeString.lower() == "range":
rangeNumbers = input("What numbers would you like to use?:")
print (np.arange(rangeNumbers))
stop = input()

最佳答案

您正在将字符串传递给不允许的函数。

>>> meanNumbers = input("What numbers would you like to use?:")
What numbers would you like to use?:1 2 3 4 5 6
>>> np.average(meanNumbers)
#...
TypeError: cannot perform reduce with flexible type

你需要用它们制作一个数组或列表。

>>> np.average(list(map(float, meanNumbers.split())))
3.5

如果您要用逗号分隔元素,请按逗号分隔。

>>> np.average(list(map(float, meanNumbers.split(','))))
    3.5

关于python - 使用 Numpy 查找输入数字集的均值、中值、众数或范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17865448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com