# Python更新numpy数组：类型错误numpy.int32不可迭代

E + S-> ES == 1E + 1S + 0ES + 0P-> 0E + 0S + 1ES + 0P

ES-> E + S == 0E + 0S + 1ES + 0P-> 1E + 1S + 0ES + 0P

ES-> E + P == 0E + 0S + 1ES + 0P-> 1E + 0S + 0ES + 1P

``````popul_num = np.array([200, 100, 0, 0])
``````

``````LHS = np.array([[1,1,0,0], [0,0,1,0], [0,0,1,0]])
``````

``````RHS = np.matrix([[0,0,1,0], [1,1,0,0], [1,0,0,1]])
``````

``````stoch_rate = np.array([0.0016, 0.0001, 0.1])
``````

``````state_change_array = RHS - LHS
``````

``````def propensity_calc(LHS, popul_num, stoch_rate):
propensity = np.zeros(len(LHS))
for row in range(len(LHS)):
a = stoch_rate[row]     # type = numpy.float64
for i in range(len(popul_num)):
if (popul_num[i] >= LHS[row, i]):
binom_rxn = binom(popul_num[i], LHS[row, i])
a = a*binom_rxn
else:
a = 0
break
propensity[row] = a     # type = numpy.ndarray
return propensity

propensity = np.zeros(len(LHS))
while tao < tmax:
propensity = propensity_calc(LHS, popul_num, stoch_rate)
a0 = (sum(propensity))
if a0 == 0.0:
break
t = np.random.exponential(1/a0)
rxn_probability = propensity / a0   # propensity = array a0 = number --> Error
num_rxn = np.arange(rxn_probability.size)
if tao + t > tmax:
tao = tmax
break
j = stats.rv_discrete(values=(num_rxn, rxn_probability)).rvs()
print(tao, t)
tao = tao + t
popul_num = popul_num + np.squeeze(np.asarray(state_change_array[j]))
``````

``````for i in range(4):
plt.plot(list(enumerate(popul_num[i])))  # error numpy.int32 object is not iterable
plt.show()
``````

EDIT: At the moment this is the plot that the code is producing

• ousman 回复

The `popul_num` are 4 values that are overwritten in each step. To draw curves, you need to save all these values in a separate array. The following code shows an example:

``````import matplotlib.pyplot as plt
import numpy as np
from scipy.special import binom
from scipy import stats

popul_num = np.array([200, 100, 0, 0])

LHS = np.array([[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]])
RHS = np.array([[0, 0, 1, 0], [1, 1, 0, 0], [1, 0, 0, 1]])
stoch_rate = np.array([0.0016, 0.0001, 0.1])
state_change_array = RHS - LHS

def propensity_calc(LHS, popul_num, stoch_rate):
propensity = np.zeros(len(LHS))
for row in range(len(LHS)):
a = stoch_rate[row]  # type = numpy.float64
for i in range(len(popul_num)):
if (popul_num[i] >= LHS[row, i]):
binom_rxn = binom(popul_num[i], LHS[row, i])
a = a * binom_rxn
else:
a = 0
break
propensity[row] = a  # type = numpy.ndarray
return propensity

tmax = 20
tao = 0.0

popul_num_all = [popul_num]

propensity = np.zeros(len(LHS))
while tao < tmax:
propensity = propensity_calc(LHS, popul_num, stoch_rate)
a0 = (sum(propensity))
if a0 == 0.0:
break
t = np.random.exponential(1 / a0)
rxn_probability = propensity / a0  # propensity = array a0 = number --> Error
num_rxn = np.arange(rxn_probability.size)
if tao + t > tmax:
tao = tmax
break
j = stats.rv_discrete(values=(num_rxn, rxn_probability)).rvs()
print(tao, t)
tao = tao + t
popul_num = popul_num + np.squeeze(np.asarray(state_change_array[j]))
popul_num_all.append(popul_num)

popul_num_all = np.array(popul_num_all)
for i, (label, color) in enumerate(zip(['Enzyme', 'Substrate', 'Enzyme-Substrate complex', 'Product'],
['limegreen', 'dodgerblue', 'orange', 'crimson'])):
plt.plot(popul_num_all[:, i], label=label, color=color)
plt.legend()
plt.tight_layout()
plt.show()
``````

添加了图例以及每个曲线的名称。

• WOW 回复

You are trying to enumerate an integer number, `popul_num[i]`, hence the error thrown. Did you mean this instead of your for loop:

``````plt.plot(popul_num)
plt.show()
``````