为什么这个python代码产生了非常大的数字?

时间:2016-10-11 02:28:37

标签: python loops random iteration

我有一个python代码,如下所示。令我不安的是我在数组rhot中作为输出得到的大数字。这不可能是正确的,因为val主要是小数字,这些小数字被行中的指数因子进一步抑制

 rhot[i, j, k] = (rhot[i, j, k] + val[j, k, l] *
                                     np.exp(-(vlist[i] - velz[j, k, l]) ** 2 / (2 * beta)))

有没有人知道为什么我会将大数字作为rhot的输出?

import numpy as np
import random

n = 6
val = np.empty((n, n, n))  #  produces n*n*n empty matrix
for i in range(0, n - 1):
    for j in range(0, n - 1):
        for k in range(0, n - 1):
            val[i, j, k] = random.lognormvariate(0, 1) 
# produces lognormal random numbers with mean 1 and standard deviation 1
velz = np.empty((n, n, n))
for i in range(0, n - 1):
    for j in range(0, n - 1):
        for k in range(0, n - 1):
            velz[i, j, k] = random.normalvariate(0, 1)
# produces normal random numbers with mean 1 and standard deviation 1
vmax = np.amax(velz)  # maximum of flat velz array
vmin = np.amin(velz)  # minimum
vlist = np.linspace(vmin, vmax, n)
# print(velz)
# print(val)
# print (vlist)
beta = 0.2  
rhot = np.empty((n, n, n))
for i in range(0, n - 1):  # index of velocity
    for j in range(0, n - 1):
        for k in range(0, n - 1):
            for l in range(0, n - 1):  # z index
                rhot[i, j, k] = (rhot[i, j, k] + val[j, k, l] *
                                 np.exp(-(vlist[i] - velz[j, k, l]) ** 2 / (2 * beta)))  # sums over 3rd dimension l, and stores the sum.
print(rhot)

1 个答案:

答案 0 :(得分:0)

我可能错了,但是当我运行你的代码时,我得到的值很小。可能是因为您正在查看-3.53107108e-310之类的内容并将其混淆为大数字吗? 那是:

-0.0 ... <307 '0's> ... 0353107108

请参阅下面的完整输出:

[[[  1.27490729e+000   2.48048100e+000   3.07058432e+000   7.30862963e-001
     3.61289051e-002   0.00000000e+000]
  [  1.29285884e+000   9.53558990e-001   1.16462089e+000   6.18595057e-001
     4.56208365e-001   0.00000000e+000]
  [  3.48843064e-002   3.51048158e-001   6.72572870e-001   1.44242671e-004
     1.08715529e-002   0.00000000e+000]
  [  1.34866696e+000   3.61798504e-002   4.94111513e-001   1.67366765e-001
     1.45956417e+000   0.00000000e+000]
  [  2.11027716e+000   1.15784556e+000   6.16183788e-002   1.58361518e-001
     5.75876010e-001   0.00000000e+000]
  [  0.00000000e+000  -2.22731705e-310   7.48734566e-315   7.48735151e-315
     8.63509807e-312   0.00000000e+000]]

 [[  8.71788238e-001   8.75201841e+000   4.64974371e+000   2.59647886e+000
     1.16994333e+000   0.00000000e+000]
  [  1.19599140e+000   8.74687167e-001   2.50158015e+000   2.75257757e+000
     9.92597288e-001   0.00000000e+000]
  [  1.84015219e+000   1.12041236e+000   3.52262073e-001   1.49498067e-001
     7.51724231e-001   0.00000000e+000]
  [  5.34739555e+000   2.75435540e+000   1.61068778e+001   1.59134588e+000
     9.67835496e-001   0.00000000e+000]
  [  2.76116504e+000   2.03916401e+000   1.90723731e-001   2.82388640e+000
     3.04602826e+000   0.00000000e+000]
  [  0.00000000e+000  -2.55325566e-310   7.48734566e-315   7.48735293e-315
     8.63509811e-312   0.00000000e+000]]

 [[  5.24104854e+000   9.34017262e+000   4.00934108e+000   3.83152870e+000
     1.62852403e+000   0.00000000e+000]
  [  3.32402433e-001   5.13142568e-001   1.56460377e+000   3.98199095e+000
     3.10752863e+000   0.00000000e+000]
  [  7.32201046e+000   4.60614519e+000   6.65947236e-001   3.50609348e+000
     2.43255192e+000   0.00000000e+000]
  [  1.20735878e+001   1.43430640e+001   1.13987763e+001   2.33564499e+000
     4.11797274e+000   0.00000000e+000]
  [  4.56305322e+000   2.19230864e+000   2.04935419e+000   5.93349070e+000
     1.34685870e+000   0.00000000e+000]
  [  0.00000000e+000  -2.87919425e-310   7.48734566e-315   7.48735435e-315
     8.63509811e-312   0.00000000e+000]]

 [[  2.00871165e+000   1.07657987e+001   1.73466587e+000   5.37829126e+000
     1.67186888e+000   0.00000000e+000]
  [  7.04409708e-001   9.18488325e-001   5.65912359e+000   3.07782156e+000
     1.90251174e+000   0.00000000e+000]
  [  2.61090422e+000   1.76056043e+000   4.02183805e+000   5.16605380e+000
     2.54000338e+000   0.00000000e+000]
  [  5.83011318e+000   3.84800152e+000   1.52000638e+000   2.67037387e+000
     3.79550774e+000   0.00000000e+000]
  [  4.66619590e-001   4.10817751e+000   5.59173904e+000   1.44794224e+000
     3.69302488e-001   0.00000000e+000]
  [  0.00000000e+000  -3.20513286e-310   7.48734566e-315   7.48734803e-315
     8.63509813e-312   0.00000000e+000]]

 [[  1.79494277e-002   3.42477445e-001   1.69493310e-002   4.18988338e-001
     2.47334739e+000   0.00000000e+000]
  [  1.06263981e-001   3.27704176e-001   6.35008094e-001   1.01981741e-001
     8.65984592e+000   0.00000000e+000]
  [  1.19169164e-001   1.93839020e-001   1.63208875e+000   1.34924978e+000
     2.13064890e+000   0.00000000e+000]
  [  7.85718993e-001   4.96021083e+000   3.80732389e-001   3.45316057e+000
     2.54260569e-001   4.11408160e-304]
  [  3.11511258e-003   3.52866964e-001   2.00264974e+000   2.47368641e-002
     6.53491661e-001   3.70335798e-318]
  [  0.00000000e+000  -3.53107108e-310   7.48734328e-315   7.48734249e-315
     8.63509820e-312   0.00000000e+000]]

 [[  0.00000000e+000  -3.58539416e-310   7.48734407e-315   7.48734234e-315
     8.63509829e-312   0.00000000e+000]
  [  0.00000000e+000  -3.63971726e-310   7.48734313e-315   7.48734866e-315
     8.63509783e-312   0.00000000e+000]
  [  0.00000000e+000  -3.69404039e-310   7.48734392e-315   7.48734265e-315
     8.63509828e-312   0.00000000e+000]
  [  0.00000000e+000  -3.74836347e-310   7.48734360e-315   7.48734249e-315
     8.63509814e-312   0.00000000e+000]
  [  0.00000000e+000  -3.80268659e-310   7.48734439e-315   7.48734234e-315
     8.63509327e-312   0.00000000e+000]
  [  0.00000000e+000  -3.85700970e-310   7.48734392e-315   7.48734249e-315
     8.63509814e-312   0.00000000e+000]]]