我正在努力实现的目标 - 我编写了这段代码,以便我可以使用统计学中的bootstrapping方法创建平均值的95%置信区间。我想将一个整数列表传递给“CI”方法,然后让方法在结尾处返回字符串。
问题 - 运行时代码不生成任何输出。请帮忙!
以下是代码:
//HomeController/Index
public ActionResult Index()
{
Model m = BusinessLayer.GetData();
return View(model);
}
答案 0 :(得分:0)
问题在于:
while x >= 0:
bootstraplist.append(hours[randint(0,6)])
这是一个无限循环,因为应该知道缩进级别是在python中创建代码块的原因,因此if
下方与while
具有相同的级别,{{1} }在if
块之外。
我不确定你的目标是什么,但是在一段固定时间内重复代码的更简单的方法是在一个范围(n)上进行for循环,其中n是重复动作的次数。在您的情况下可以是这样的
while
(使用for _ in range(5):
bootstraplist.append(hours[randint(0,6)])
是变量的约定,我们不关心它的值和/或我们不使用它)
或者你可以使用list comprehension直接创建列表
_
另外,通过你使用它的方式而不是randint,choice
是更好的选择
bootstraplist = [hours[randint(0,6)] for _ in range(5) ]
其他方面,最好将所有导入作为代码中的第一件事。
将这一点与评论一起考虑,也许这就是你想要的?
bootstraplist = [choice(hours) for _ in range(5) ]
测试
import numpy
from random import choice
class bootstrapping(object):
def __init__(self,hours=[]):
self.hours = hours
def CI(self):
listofmeans = []
for numbers in range(0,1000):
bootstraplist = [ choice(self.hours) for _ in range(5) ]
listofmeans.append(sum(bootstraplist) / len(bootstraplist))
s = numpy.std(listofmeans)
z = 1.96
lower_confidence = (sum(listofmeans) / len(listofmeans)) - z*s
upper_confidence = (sum(listofmeans) / len(listofmeans)) + z*s
return "Lower confidence:",lower_confidence,"Upper confidence:",upper_confidence