【发布时间】:2018-05-14 09:29:56
【问题描述】:
我有一个 for 循环,通过它我可以为每个月生成一些随机的每日值。我需要确保生成的每月平均值应等于观察到的平均值(在不同的数据框中提供)。所以我需要运行这个 for 循环,直到生成所需的值,然后再运行下个月。
我有示例代码,但不确定如何多次运行它直到获得所需的值:
for j in np.arange(1,13,1):
# calculating monthly leanth from provided datasets
mlen = ((df_test[df_test.index.year==2000]).index.month==j).sum()
# fit to my distribution
phat = stats.exponweib.fit((df_test[df_test.index.month==j].mean(axis=1)).dropna())
# predicting based on the fitted distribution
esti = stats.exponweib.rvs(*phat,int(mlen))
if esti.mean() == monthly_mean[0]
monthly_mean 是观察到的每月平均值,如下所示,其中第一个月的第一个值。
monthly_mean = array([ 5.15 , 5.948571, 7.028261, 4.144231, 3.585965,
5.244828, 4.915455, 4.757 , 5.803614, 12.573684,
5.683333, 3.875 ])
非常感谢任何帮助/建议!
【问题讨论】:
-
你应该使用while循环
-
@Dipu 或 1 个 for 循环
-
@Dipu 代替 if 或代替 for 循环?
标签: python-3.x numpy for-loop if-statement