【发布时间】:2017-12-06 10:26:25
【问题描述】:
我正在尝试返回一个加起来为 100 .. 11 次的数字列表。
有 3 个数字是从 numpy 随机均匀分布生成的。
我想添加一个 if 语句来查看每个列表的第 1、第 2 和第 3 个数字(总共 11 个).. 如果绘制的 Pearson 相关系数 >0.99。
目前,我只能生成 1 个总和等于 100 的数字列表。
我有以下代码:
import math
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
c1_high = 98
c1_low = 75
c2_high = 15
c2_low = 6
c3_high = 8
c3_low = 2
def mix_gen():
while True:
c1 = np.random.uniform(c1_low, c1_high)
c2 = np.random.uniform(c2_low, c2_high)
c3 = np.random.uniform(c3_low, c3_high)
tot = c1+c2+c3
if 99.99<= tot <=100.01:
comp_list = [c1,c2,c3]
return comp_list
my_list = mix_gen()
print(my_list)
所以如果我要绘制每个组件.. 例如 c1... 我会得到 >0.99 的 R^2 值。
我被困在同一个函数中生成多个列表。我知道这可以在函数之外完成.. 使用 [mix_gen() for _ in range(11)].. 但这不起作用,因为我需要在返回 11 个列表之前对 peasron corr coeff 进行额外检查。
目标:
返回具有以下值的数据框:
C1 C2 C3 sum
1 70 20 10 100
2 ..
3 ..
4 ..
5 ..
6 ..
7 ..
8 ..
9 ..
10 ..
11 90
R^2 1 1 1
【问题讨论】:
-
在
if中使用计数器标志,并且在将 11 个列表附加到容器之前不要从while返回 -
也许你正在寻找
yield
标签: python pandas loops numpy pearson-correlation