变量值的变化取决于它是第一次还是第二次执行

时间:2018-04-24 08:13:01

标签: python pycharm

我有一个非常具体的问题,我甚至无法搜索类似的问题。

我有以下变量

cm = np.array([[65, 4, 22, 24],
               [6, 81, 5, 8],
               [0, 11, 85, 19],
               [4, 7, 3, 90]])

proportions = [0.3, 0.4, 0.1, 0.2]

n = np.sum(cm)
nisum = np.sum(cm, axis=1)
nsumj = np.sum(cm, axis=0)

p_i = np.zeros([cm.shape[0], 1])
p_ii = np.zeros([cm.shape[0], cm.shape[1]])
prod_ac = np.zeros([cm.shape[0], 1])
user_ac = np.zeros([cm.shape[0], 1])
overall_ac = 0

for i in range(cm.shape[1]):
    for j in range(cm.shape[0]):
        p_ii[i, j] = proportions[i] * cm[i, j] / nisum[i]

    if i == 0:
        p_i = np.sum(p_ii, axis=0)

    prod_ac[i] = ((proportions[i] / p_i[i]) * (cm[i, i] / nisum[i]))
    user_ac[i] = cm[i, i] / nisum[i]
    overall_ac += proportions[i] * cm[i, i] / nisum[i]

如果我在PyCharm的控制台中执行整个代码块,则prod_ac的结果是错误的,结果

prod_ac
Out[215]: 
array([[  1.        ],
   [ 31.05      ],
   [  1.28787879],
   [  2.76442308]])

但是如果我只是再次执行代码的循环部分,那么prod_ac的结果就可以了:

prod_ac
Out[217]: 
array([[ 0.84252858],
   [ 0.90639122],
   [ 0.47056319],
   [ 0.60898115]])

没有错误或警告上升。

我猜它是记忆的东西。显然,如果我一次执行所有行,结果是错误的。但是如果我执行第一部分(从第1行到循环之前的行),然后我执行循环,结果很好。

每次都创建所有其他变量。这只是带有麻烦的prod_ac。

有没有人知道问题在哪里?我怎么能每次都能获得好成绩。这将是一个函数的一部分,所以我将来不能执行部分。

感谢

1 个答案:

答案 0 :(得分:1)

这不是内存问题。问题出在p_i

import numpy as np
cm = np.array([[65, 4, 22, 24],
               [6, 81, 5, 8],
               [0, 11, 85, 19],
               [4, 7, 3, 90]])

proportions = [0.3, 0.4, 0.1, 0.2]

n = np.sum(cm)
nisum = np.sum(cm, axis=1)
nsumj = np.sum(cm, axis=0)

p_i = np.zeros([cm.shape[0], 1])
p_ii = np.zeros([cm.shape[0], cm.shape[1]])
prod_ac = np.zeros([cm.shape[0], 1])
user_ac = np.zeros([cm.shape[0], 1])
overall_ac = 0

for i in range(cm.shape[1]):
    for j in range(cm.shape[0]):
        p_ii[i, j] = proportions[i] * cm[i, j] / nisum[i]

    if i == 0:
        p_i = np.sum(p_ii, axis=0)
        print(p_i)

    prod_ac[i] = ((proportions[i] / p_i[i]) * (cm[i, i] / nisum[i]))
    user_ac[i] = cm[i, i] / nisum[i]
    overall_ac += proportions[i] * cm[i, i] / nisum[i]

for i in range(cm.shape[1]):
    for j in range(cm.shape[0]):
        p_ii[i, j] = proportions[i] * cm[i, j] / nisum[i]

    if i == cm.shape[1]-1:
        p_i = np.sum(p_ii, axis=0)
        print(p_i)

    prod_ac[i] = ((proportions[i] / p_i[i]) * (cm[i, i] / nisum[i]))
    user_ac[i] = cm[i, i] / nisum[i]
    overall_ac += proportions[i] * cm[i, i] / nisum[i]

会给出

[0.16956522 0.01043478 0.0573913  0.0626087 ]
[0.20125753 0.35746154 0.15707358 0.28420736]

因此造成了差异。原因是你在第i轴完成之前在第i轴上求和。所以它第二次工作是有意义的,因为这次整个数组已经准备就绪,所以它可以使用在下面的迭代中生成的值。如何解决这个问题取决于你。一个工作示例如下:

for i in range(cm.shape[1]):
    for j in range(cm.shape[0]):
        p_ii[i, j] = proportions[i] * cm[i, j] / nisum[i]

p_i = np.sum(p_ii, axis=0)

for i in range(cm.shape[1]):
  prod_ac[i] = ((proportions[i] / p_i[i]) * (cm[i, i] / nisum[i]))
  user_ac[i] = cm[i, i] / nisum[i]
  overall_ac += proportions[i] * cm[i, i] / nisum[i]