[英]Why am I getting “ValueError: setting an array element with a sequence.” when using the brute function from Scipy.optimization?
Working on a model to predict electrophysiological data given a set of parameters. 在给定一组参数的情况下对模型进行预测电生理数据的工作。 This script is trying to find values for those parameters that give predictions closest to experimental data.
该脚本试图为那些提供最接近实验数据的预测的参数寻找值。 I'm running Python 2.7, Scipy 0.17.0, and Numpy 1.10.4.
我正在运行Python 2.7,Scipy 0.17.0和Numpy 1.10.4。 The script is attached below.
该脚本附在下面。 The line that is getting the error is
epsc_sims[n,1] = r_prob*poolsize
. 出现错误的行是
epsc_sims[n,1] = r_prob*poolsize
。
Here is the script: 这是脚本:
import scipy.optimize as optimize
import numpy as np
import math
def min_params(*params):
std_err = 0
epsc_exp = np.loadtxt('sample.txt')
max_pool = params[0]
r_prob = params[1]
tau_recov = params[2]
poolsize = epsc_exp[0,1]/r_prob
epsc_sims = np.copy(epsc_exp)
count = epsc_exp.size
for n in xrange(1 , count/2):
poolsize = poolsize - epsc_sims[n-1, 1]
poolsize = max_pool + (poolsize - max_pool) * math.exp((epsc_sims[n-1, 0] - epsc_sims[n,0]) / tau_recov)
epsc_sims[n,1] = r_prob*poolsize
std_err += (epsc_exp[n,1] - epsc_sims[n,1])**2
std_err /= count
return std_err
params = (1e-8, 0.2, 0.5)
rranges = (slice(5e-9,5e-8,1e-9), slice(0.1, 0.3, 0.01), slice(0.3, 0.4, 0.01))
y = optimize.brute(min_params, rranges, args = params)
print y
And here is the Traceback (most recent call last): 这是Traceback(最近一次通话):
Traceback (most recent call last):
File "<ipython-input-25-21d343f36a44>", line 1, in <module>
runfile('C:/Users/brennan/Google Drive/Python Scripts/Inhibitory Model/brute.py', wdir='C:/Users/brennan/Google Drive/Python Scripts/Inhibitory Model')
File "D:\Python\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "D:\Python\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/brennan/Google Drive/Python Scripts/Inhibitory Model/brute.py", line 33, in <module>
y = optimize.brute(min_params, rranges, args = params)
File "D:\Python\Anaconda2\lib\site-packages\scipy\optimize\optimize.py", line 2604, in brute
Jout = vecfunc(*grid)
File "D:\Python\Anaconda2\lib\site-packages\numpy\lib\function_base.py", line 1811, in __call__
return self._vectorize_call(func=func, args=vargs)
File "D:\Python\Anaconda2\lib\site-packages\numpy\lib\function_base.py", line 1874, in _vectorize_call
ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
File "D:\Python\Anaconda2\lib\site-packages\numpy\lib\function_base.py", line 1836, in _get_ufunc_and_otypes
outputs = func(*inputs)
File "D:\Python\Anaconda2\lib\site-packages\scipy\optimize\optimize.py", line 2598, in _scalarfunc
return func(params, *args)
File "C:/Users/brennan/Google Drive/Python Scripts/Inhibitory Model/brute.py", line 25, in min_params
epsc_sims[n,1] = r_prob*poolsize
ValueError: setting an array element with a sequence.
The text file I use for spikes = np.loadtxt('sample.txt')
is formatted as follows with ~3,000 lines: 我用于
spikes = np.loadtxt('sample.txt')
的文本文件的格式设置为约3,000行:
0.01108 1.223896e-08
0.03124 6.909375e-09
0.074 6.2475e-09
0.07718 3.895625e-09
This is my first post on here so please let me know if I need to change anything or provide more info! 这是我在这里的第一篇文章,因此,如果我需要更改任何内容或提供更多信息,请告诉我!
The scipy.optimize
routines call the function with a vector of parameters to be optimized. scipy.optimize
例程使用要优化的参数向量调用该函数。 Your function is thus called as min_params(x, *params)
, where the *params
are your custom arguments to the function that you supplied to the function using the keyword argument args
. 因此,您的函数称为
min_params(x, *params)
,其中*params
是使用关键字参数args
提供给函数的函数的自定义参数。 The way you define your function x
will end up as the first elements of params
inside the function. 定义函数
x
方式最终将成为函数内部params
的第一个元素。
Assuming that max_pool
, r_prob
, tau_recov
are what you want to optimize over here is how to fix things: 假设
max_pool
, r_prob
, tau_recov
你想在这里过优化是什么如何解决的事情:
def min_params(params):
...
y = optimize.brute(min_params, rranges)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.