如何在Python中创建只有一个值的正确直方图

问题描述 投票:0回答:1

我写了一个函数 aveMean(die, numRolls, numTrials) ,它需要以下内容:

块引用

  • 死了,死了
  • numRolls、numTrials 是正整数
  • 计算 numRolls 卷的 numTrials 次运行中某个数字的最长运行的预期平均值。
  • 调用 makeHistogram 生成所有试验的最长运行的直方图。直方图中应该有 10 个 bin
  • 为 x 轴和 y 轴选择适当的标签。
  • 返回计算的平均值

块引用

一切正常,除了:

  1. 列表只有一项

  1. 列表中的项目是相等的

  1. 或者 numRolls 仅限一卷

它应该是这样的

你在 1.-3 中看到了这一点。左侧总是有一个额外的小栏(几乎看不见)。我怎样才能摆脱它?我读到一些关于这是由于Python 3造成的,但我没有找到解决方案。

谢谢!

附注我编辑了如何调用直方图的代码以及使用调用创建直方图的函数:

def getMeanAndStd(X):
    mean = sum(X)/float(len(X))
    tot = 0.0
    for x in X:
        tot += (x - mean)**2
    std = (tot/len(X))**0.5
    return mean, std

class Die(object):
    def __init__(self, valList):
        """ valList is not empty """
        self.possibleVals = valList[:]
    def roll(self):
        return random.choice(self.possibleVals)

def makeHistogram(values, numBins, xLabel, yLabel, title=None):
    pylab.hist(values, numBins)
    pylab.xlabel(xLabel)
    pylab.ylabel(yLabel)
    if(title != None): pylab.title(title)
    pylab.show()

def aveMean(die, numRolls, numTrials):
    tries = die
    testList, res = [], []
    for i in range(numTrials):
        count, tempCount, testList = 1, 1, []
        for i in range(numRolls):
            testList.append(tries.roll())
        for i in range(1, numRolls):
            if testList[i-1] == testList[i]:
                count +=1
            else:
                if count > tempCount:
                    tempCount = count
                    count = 1
                else:
                    count = 1
        res.append(tempCount)
    mean, std = getMeanAndStd(res)
    makeHistogram(res, 10, 'Quantity of Consecutive Numbers', 'Consecutive Number per Run')
    return round(mean, 3)

我收到的错误消息是:

Unsuccessfully called makeHistogram

python list histogram
1个回答
0
投票
def getAverage(die, numRolls, numTrials):
    """
      - die, a Die
      - numRolls, numTrials, are positive ints
      - Calculates the expected mean value of the longest run of a number
        over numTrials runs of numRolls rolls
      - Calls makeHistogram to produce a histogram of the longest runs for all
        the trials. There should be 10 bins in the histogram
      - Choose appropriate labels for the x and y axes.
      - Returns the mean calculated
    """
    tries = die
    testList, res = [], []
    for i in range(numTrials):
        count, tempCount, testList = 1, 1, []
        for i in range(numRolls):
            testList.append(tries.roll())
        for i in range(1, numRolls):
            if testList[i-1] == testList[i]:
                count +=1
            else:
                count = 1
                if count > tempCount:
                    tempCount = count
            if count > tempCount:
                tempCount = count
        res.append(tempCount)
    mean, std = getMeanAndStd(res)
    makeHistogram(res, 10, 'Quantity of Consecutive Numbers', 'Consecutive Number per Run')
    return round(mean, 3)```
© www.soinside.com 2019 - 2024. All rights reserved.