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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17865448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:20:49  来源:igfitidea点击:

Using Numpy to find Mean,Median,Mode or Range of inputted set of numbers

pythonnumpy

提问by Hartbypass

I am creating a program to find Mean,Median,Mode, or Range. When I run this it works fine until it gets to the part of calculating the answer. It gives me a "cannot preform reduce with flexible type" error. I have searched this error but could not find what I needed to fix. This is my first time using numpy so any help would be great.

我正在创建一个程序来查找均值、中值、众数或范围。当我运行它时,它工作正常,直到它进入计算答案的部分。它给了我一个“不能用灵活类型减少预成型”的错误。我搜索了这个错误,但找不到我需要修复的内容。这是我第一次使用 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()

采纳答案by Sukrit Kalra

You are passing a string to the functions which is not allowed.

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

>>> 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

You need to make an array or a list out of them.

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

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

IF you're seperating the elements by commas, split on the commas.

如果您用逗号分隔元素,请在逗号上拆分。

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

回答by SethMMorton

This is not an answer (see @Sukrit Kalra's response for that), but I see an opportunity to demonstrate how to write cleaner code that I cannot pass up. You have a large amount of code duplication that will result in difficult to maintain code in the future. Try this instead:

这不是答案(请参阅@Sukrit Kalra 对此的回应),但我看到了一个机会来演示如何编写我不能错过的更清晰的代码。您有大量的代码重复,这将导致将来难以维护代码。试试这个:

import sys
import numpy as np

welcomeString = input("Welcome to MMMR Calculator\nWhat would you like to calculate(Mean,Median,Mode,Range):")
welcomeString = welcomeString.lower() # Lower once and for all

# All averages need to do this
numbers = input("What numbers would you like to use?:")
numbers = list(map(float, numbers.split(','))) # As per Sukrit Kalra's answer

# Use a map to get the function you need
average_function = { "mean": np.average,
                     "median": np.median,
                     "mode": np.mode,
                     "range": np.arange,
                   } 

# Print the result of the function by passing in the
# pre-formatted numbers from input
try:
    print (average_function[welcomeString](numbers))
except KeyError:
    sys.exit("You entered an invalid average type!")

input() # Remove when you are done with development