ValueError: too many values to unpack (expected 3)?

2024/7/4 10:38:49

I have been having issues with the code I am trying to right with the model I am trying to code the following error has appeared and being a relative novice I am unsure of how to resolve it.

ValueError                                Traceback (most recent call last)
<ipython-input-2-5f21a0ce8185> in <module>()26         proposed[j] = proposed[j] + np.random.normal(0,propsigma[j])27         if (proposed[j]>0): # automatically reject moves if proposed parameter <=0
---> 28             alpha = np.exp(logistic_loglik(proposed,time,ExRatio,sig)-logistic_loglik(par_out[i-1,],time,ExRatio,sig))29             u = np.random.rand()30             if (u < alpha):<ipython-input-2-5f21a0ce8185> in logistic_loglik(params, t, data, sig)3 # set up a function to return the log likelihood4 def logistic_loglik(params,t,data,sig):
----> 5     return sum(norm.logpdf(logistic(data, t, params),sig))6 7 # set standard deviations to be 10% of the population values<ipython-input-1-c9480e66b7ef> in logistic(x, t, params)6 7 def logistic(x,t,params):
----> 8     S, R, A = x9     r, Nmax, delta_s, beta, gamma, delta_r, delta_a, Emax, H, MICs, MICr = params10     N = S + RValueError: too many values to unpack (expected 3) 

The model I am trying to code is an MCMC to fit some ODE's to some data I have added the code below for context.

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint%matplotlib inlinedef logistic(x,t,params):    S, R, A = xr, Nmax, delta_s, beta, gamma, delta_r, delta_a, Emax, H, MICs, MICr = paramsN = S + RE_s = 1 - (Emax * A**H)/(MICs**H + A**H)E_r = 1- (Emax * A**H)/(MICr**H + A**H)derivs = [r * (1 - N / Nmax ) * E_s * S - delta_s * S - ((beta * S * R)/N), r * (1 - gamma) * (1 - N/Nmax) * E_r * R  - delta_r * R + ((beta * S * R)/N), - delta_a * A]return derivsr = 0.5
Nmax = 10**7
delta_s = 0.025
beta = 10**-2
gamma = 0.5
delta_r = 0.025
delta_a = 0.003
Emax = 2
H = 2
MICs = 8
MICr = 2000[r, Nmax, delta_s, beta, gamma, delta_r, delta_a, Emax, H, MICs, MICr] = params
S = 9 * 10**6
R = 10**5
A = 5.6
x0 = [S, R, A]maxt = 2000
tstep = 1
t = np.arange(0,maxt,tstep)def logistic_resid(params,t,data):return logistic(params,t)-datalogistic_out = odeint(logistic, x0, t, args=(params,))time = np.array([0, 168, 336, 504, 672, 840, 1008, 1176, 1344, 1512, 1680, 1848, 2016, 2184, 2352, 2520, 2688, 2856])
ExRatio = np.array([2, 27, 43, 36, 39, 32, 27, 22, 13, 10, 14, 14, 4, 4, 7, 3, 3, 1])
ratio = 100* logistic_out[:,1]/(logistic_out[:,0]+logistic_out[:,1])
plt.plot(t,ratio)
plt.plot(time,ExRatio,'h')
xlabel('Position')
ylabel('Pollution')

New Cell

from scipy.stats import norm# set up a function to return the log likelihood
def logistic_loglik(params,t,data,sig):return sum(norm.logpdf(logistic(data, t, params),sig))# set standard deviations to be 10% of the population values
sig = ExRatio/10# parameters for the MCMC
reps = 50000
npars = 3# output matrix
par_out = np.ones(shape=(reps,npars))
# acceptance 
accept = np.zeros(shape=(reps,npars))
# proposal standard deviations. These have been pre-optimized.
propsigma = [0.05,20,5]for i in range(1,reps):# make a copy of previous parameterspar_out[i,] = par_out[i-1,]for j in range(npars):proposed = np.copy(par_out[i,:]) # we need to make a copy so that rejected moves don't affect the original matrixproposed[j] = proposed[j] + np.random.normal(0,propsigma[j])if (proposed[j]>0): # automatically reject moves if proposed parameter <=0 alpha = np.exp(logistic_loglik(proposed,time,ExRatio,sig)-logistic_loglik(par_out[i-1,],time,ExRatio,sig))u = np.random.rand()if (u < alpha):par_out[i,j] = proposed[j]accept[i,j] = 1#print(sum(accept[range(101,reps),:])/(reps-100))#plt.plot(par_out[:,0])
#plt.plot(par_out[range(101,reps),0])
#plt.plot(par_out[:,0],par_out[:,2])
plt.hist(par_out[range(101,reps),0],50)
print('\n')
a=np.mean(par_out[range(101,reps),0])

I think its mistaking my parameters for something else but that might be wrong. I am using Jupyter notebook

Answer

You cannot use S, R, A = x, if x is empty or has not enough (too much) values to unpack.

For what I see, you are trying to define S, R and A values using the variable x. It is possible in that way only if x is the len of 3. If you want to assign certain x values to specific S, R or A use loop, or if you want to do this that way you can use:

S, R, *A = x,

this way the variable S and R will have the first and second element of x, and variable A the rest. You can put * before any variable to make it take the excessive values you store in x.

https://en.xdnf.cn/q/120161.html

Related Q&A

Finding a hello word in a different string, which it has hello in it

I should find a hello word in a string, which I gave it from input. Here is the code that I currently have, but I cannot match hello with the character list.mylist = it can be any letter plus hello in …

Building Permutation with Python

Im trying to write a programme to get all permutations of a string of letter using recursion. As Im a beginner in Python, I learnt about recursion with examples like Fibonacci Number and Factorial. I u…

Python: Is There a builtin that works similar but opposite to .index()?

Just a forewarning: I just recently started programming and Python is my first language and only language so far.Is there a builtin that works in the opposite way of .index()? Im looking for this beca…

Get a subset of a data frame into a matrix

I have this data frame:I want just the numbers under August - September to be placed into a matrix, how can I do this?I tried this cf = df.iloc[:,1:12] which gives me but it gives me the headers as w…

Get Pairs of List Python [duplicate]

This question already has answers here:How can I iterate over overlapping (current, next) pairs of values from a list?(13 answers)Closed 9 years ago.c = [1,2,3,4,5,6,7,8,9,10]for a,b in func(c):doSome…

Python - make a list

How can I turn something like this: (not a file)Edit: Its what I get when I print this:adjusted_coordinate_list = str(adjusted_X_coordinate) + , + str(Y_coordinate)1,1 1,2 1,3 1,4 1,5into this:[1,1,1,2…

Error while running test case

I need to test my code below. I am using one test to see if it is working or not. but dont know what exactly I should pass as a parameter in the test code. Please see the test code at the end and pleas…

How to run something on each line of txt file?

I am trying to get this to run on each line of a .txt file.D1 =int(lines[0])*10 D2 =int(lines[1])*9 D3 =int(lines[2])*8 D4 =int(lines[3])*7 D5 =int(lines[4])*6 D6 =int(lines[5])*5 D7 =int(lines[6])*4 D…

Python np.nditer() - ValueError: Too many operands

I have a few methods which pass different amount of messy data to this function to combine headers with data and return a list of dictionaries:def zip_data(self, indicator_names, indicator_values):valu…

Python 3 - Find the Mode of a List

def mode(L):shows = []modeList = []L.sort()length = len(L)for num in L:count = L.count(num)shows.append(count)print List = , LmaxI = shows.index(max(shows))for i in shows:if i == maxI:if modeList == []…