Python数组仅为1D,我无法使用重塑将其转换为2D

时间:2020-07-10 13:44:07

标签: python arrays reshape numpy-ndarray

我创建了两个程序来对化学反应系统进行随机模拟。在程序中,我有一个函数,该函数要使用derivative的变化分子数popul_num和每个反应的随机速率常数stoch_rate更新数组的元素。第一个程序的功能如下:

popul_num = np.array([1.0E9, 0, 0])
stoch_rate = np.array([1.0, 0.002, 0.5, 0.04])

def update_array(popul_num, stoch_rate): 
    """Specific to this model 
    will need to change if different model 
    implements equaiton 24 of the Gillespie paper""" 
    # calcualte in seperate varaible then pass it into the array 
    s_derviative = stoch_rate[1]*(2*popul_num[0] -1)/2
    b = np.array([[1.0, 0.0, 0.0], [s_derviative, 0.0, 0.0], [0.0, 0.5, 0.0], [0.0, 0.4, 0.0]])
    return b 

此函数返回b的{​​{1}}的{​​{1}}

在下一个程序中,我添加了更多的反应和更多的反应物,其功能如下:

array

仅此方法返回shape(4, 3)的{​​{1}},而我需要将其作为popul_num = np.array([1.0E5, 3.0E5, 0.0, 1.0E5, 0.0, 0.0, 0.0, 0.0]) stoch_rate = np.array([0.015, 0.00016, 0.5, 0.002, 0.002, 0.8]) def update_array(popul_num, stoch_rate): """Specific to this model will need to change if different model implements equaiton 24 of the Gillespie paper""" s_derivative = stoch_rate[0]*popul_num[1]*((popul_num[1] - 1)/2) # derivative with respect to S is a function of X x_derivative = stoch_rate[0]*popul_num[0]*((2*popul_num[1] - 1)/2) # derivative with respect to X is a function of S r_derivative = stoch_rate[1]*((popul_num[3]*(popul_num[3]))/2) r2_derivative = stoch_rate[2]*popul_num[4] # derivative with respect to R is a function of Y type = numpy.float64 y_derivative = stoch_rate[3]*popul_num[3] # derivative with respect to Y is a function of R type = numpy.float64 x2_derivative = stoch_rate[4]*((popul_num[1] - 1)/2) b = np.array([[x_derivative, s_derivative, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, r_derivative, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, r2_derivative, y_derivative, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, stoch_rate[3], 0.0, 0.0, 0.0, 0.0], [x2_derivative, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, stoch_rate[5], 0.0]]) b.reshape((6,8)) print("Shape b:\n", b.shape) return b 的2D数组,但我尝试使用array方法,但这导致出现以下错误:

shape(6,)

在我调用shape(6, 8)命令的行上抛出了

我不明白第二个函数有什么不同,这意味着它不返回2D数组吗?

欢呼

1 个答案:

答案 0 :(得分:1)

缺少一个值。该列表仅包含7个值:

 [x2_derivative, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]