【问题标题】:displaying a table without invalid values pythondisplaying a table without invalid values python
【发布时间】:2022-12-01 23:27:08
【问题描述】:

I have the following data on the joint probabilities for two random variables:

N = 116
n = 25
q = 0.05
I = stats.binom(N,q)
D2 = [stats.hypergeom(N,K,n) for K in range(0,117)]
pmf_I = [I.pmf(i).round(4) for i in np.arange(0,117)]
conditional_pmf = [[D2[i].pmf(d).round(4) for d in np.arange(0,26)] for i in np.arange(0,117)]

joint_pmf_ID = [[(pmf_I[i] * conditional_pmf[i][d]).round(4) for d in np.arange(0,26)
                 if ((pmf_I[i] * conditional_pmf[i][d]).round(4))!= 0]
                for i in np.arange(0,117) if pmf_I[i] != 0]


for i in range(len(joint_pmf_ID)):
    for j in range(len(joint_pmf_ID[9])):
        if j == len(joint_pmf_ID[i]) - 1 and j != 6:
            joint_pmf_ID[i].append(0)
        else:
            continue

joint_pmf_ID

[[0.0026, 0, 0, 0, 0, 0, 0],
[0.0125, 0.0034, 0, 0, 0, 0, 0],
 [0.0295, 0.0164, 0.0022, 0, 0, 0, 0],
 [0.0462, 0.0389, 0.0104, 0.0009, 0, 0, 0],
 [0.0535, 0.0607, 0.0246, 0.0042, 0.0003, 0, 0],
 [0.0489, 0.0703, 0.0384, 0.0099, 0.0012, 0.0001, 0],
 [0.0369, 0.0644, 0.0444, 0.0155, 0.0029, 0.0003, 0],
 [0.0236, 0.0486, 0.0407, 0.0179, 0.0045, 0.0006, 0],
 [0.013, 0.0311, 0.0307, 0.0164, 0.0052, 0.001, 0.0001],
 [0.0063, 0.0172, 0.0196, 0.0124, 0.0048, 0.0011, 0.0002],
 [0.0027, 0.0083, 0.0108, 0.0079, 0.0036, 0.0011, 0.0002],
 [0.0011, 0.0036, 0.0053, 0.0044, 0.0023, 0.0008, 0.0002],
 [0.0004, 0.0014, 0.0023, 0.0021, 0.0013, 0.0005, 0.0001],
 [0.0001, 0.0005, 0.0009, 0.0009, 0.0006, 0.0003, 0.0001],
 [0.0002, 0.0003, 0.0003, 0.0003, 0.0001, 0, 0],
 [0.0001, 0.0001, 0.0001, 0.0001, 0, 0, 0],
 [0.0001, 0, 0, 0, 0, 0, 0]]

I've added in zeros so that I can display the data with looping functionality. Now Ideally I'd like to display only values that are above, say, 0.05, the rest being meaninglessly different from zero. The only way I have been able to do this is in the following extremely piece meal bit of code:

print("Joint probability distribution:")
print('|\t|',end='')
for y in range(len(values_D) - 1):
    print(f' D={y}\t|', end='')
print()
for i in range(len(values_I) - 3):
    print(f'| I={i}\t|', end='')
    for j in range(len(values_D) - 1):
        print(f'{joint_pmf_ID[i][j]:.3f}\t|', end='')
    print()
print()

Is there a better way to do this? It's hard because my initial data was a ragged list, so I had to just add zeros, and now it's all very awkward.

【问题讨论】:

  • Do you need to print it? Otherwise I'd suggest something like matplotlib.pyplot.hist2d see Examples
  • The reason to print it was that I'd need a 3d visualisation to do it graphically, which was more effort than its worth compared to a table

标签: python numpy statistics


【解决方案1】:

I'm not sure I fully understand what format you're trying to do, but hope ths helps point you in the right direction.

If you want to print only the values where val > 0.05:

  1. Make your joint_pmf_ID a numpy array.
  2. Index by boolean condition and set the desired values to 0.
  3. In your looping function, don't print zeros.
    joint_pmf_ID = np.array(joint_pmf_ID)
    joint_pmf_id[joint_pmf_id<=0.05] = 0
    # Below is just a fancy print function; print how you will
    for i, row in enumerate(joint_pmf_ID):
        print('| ')
        for j, val in enumerate(row):
            print(val if val else ' ')
            print(' | ')
    

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 2022-12-02
    • 2010-10-26
    • 1970-01-01
    • 2014-10-26
    • 2020-12-15
    相关资源
    最近更新 更多