I'm trying to multiply two arrays together element wise
expected_state = np.array([-1.004 0.002 0.0])
b = np.array([[1.0, 0.0, 0.0], [[stoch_rate[1]*(2*(popul_num[0]) - 1)/2], 0.0, 0.0], [0.0, 0.5, 0.0], [0.0, 0.4, 0.0]])
Each element of expected_state
should be multiplied postionally with every element from each row in b
So [[-1.004*1.0, 0.002*0.0, 0.0*0.0], [-1.004*[stoch_rate[1]*(2*(popul_num[0]) - 1)/2], 0.002*0.0....etc]]
Array b
is defined in a function so that the first element in row two can change as stoch_rate
and popul_num
change as the program executes.
def update_matrix(popul_num, stoch_rate):
"""Specific to this model
will need to change if different model
implements equaiton 24 of the Gillespie paper"""
b = np.array([[1.0, 0.0, 0.0], [[stoch_rate[1]*(2*(popul_num[0]) - 1)/2], 0.0, 0.0], [0.0, 0.5,
0.0], [0.0, 0.4, 0.0]])
return b
So far I've used nested for loops to try and do the multiplication:
for j in range(len(evaluate_propensity)):
for i in range(len(popul_num)):
denominator[j] += (exptd_state_array[i]*b[j, i]) # TypeError: Cant multiply sequence by non-int type "numpy.float64"
But get the TypeError: can't multiply sequence by non-int of type 'numpy.float64'
I've had a look at some other posts which say things like this happen when trying to multiply list indecies with non-integers because list indecies can't have partial numbers. Which I understand but the elements of my arrays are meant to be floats so I'm not too sure how to over come that.
Then after reading some more, I found that the error came when the program was trying to do the multiplication of the [stoch_rate[1]*(2*(popul_num[0]) - 1)/2]
element from array b
and was wondering if the TypeError
would come from that formula derived element of the array and if it does how could that be fixed?
Cheers
EDIT:
popul_num = np.array([1.0E5, 0, 0]) # array of molecule numbers for 3 species in model
stoch_rate = np.array([1.0, 0.002, 0.5, 0.04]) # rates of the 4 reactions in the model
evaluate_propensity = np.array(a, b, c, d) # An array of the probability of each reaction occuring, is dynamically calculated on each iteration so isn't hard coded.
exptd_state_array and expected_state are the same thing sorry forgot to change the short hand
popul_num = np.array([1.0E5, 0, 0]) # array of molecule numbers for 3 species in model
stoch_rate = np.array([1.0, 0.002, 0.5, 0.04]) # rates of the 4 reactions in the model
evaluate_propensity = np.array((.25,.25,.25,.25)) # An array of the probability of each reaction occuring, is dynamically calculated on each iteration so isn't hard coded.
denominator = np.zeros(4,)
expected_state = np.array([-1.004, 0.002, 0.0])
exptd_state_array = expected_state
b = np.array([[1.0, 0.0, 0.0], [[stoch_rate[1]*(2*(popul_num[0]) - 1)/2], 0.0, 0.0], [0.0, 0.5, 0.0], [0.0, 0.4, 0.0]])
b
array([[1.0, 0.0, 0.0],
[list([199.999]), 0.0, 0.0],
[0.0, 0.5, 0.0],
[0.0, 0.4, 0.0]], dtype=object)
so, b has mixed types. The list is generated by the square brackets around [stoch_rate[1]*(2*(popul_num[0]) - 1)/2]
Multiplication for lists is defined as concatenation with itself: 3 * [5] = [5, 5, 5]
. This fails with floats, as @hpaulj pointed out in the comment.
leaving out the square brackets:
b = np.array([[1.0, 0.0, 0.0], [stoch_rate[1]*(2*(popul_num[0]) - 1)/2, 0.0, 0.0], [0.0, 0.5, 0.0], [0.0, 0.4, 0.0]])
b
array([[ 1. , 0. , 0. ],
[199.999, 0. , 0. ],
[ 0. , 0.5 , 0. ],
[ 0. , 0.4 , 0. ]])
Then, the double loop does execute
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.