为什么我得到" ValueError:用序列设置数组元素。"什么时候使用Scipy.optimization的暴力函数?

时间:2016-08-03 22:56:37

标签: python numpy scipy scientific-computing

使用模型在给定一组参数的情况下预测电生理数据。此脚本正在尝试查找那些给出最接近实验数据的预测的参数的值。我正在运行Python 2.7,Scipy 0.17.0和Numpy 1.10.4。该脚本附在下面。获得错误的行是epsc_sims[n,1] = r_prob*poolsize

这是脚本:

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

这是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.

我用于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

这是我在这里发表的第一篇文章,如果我需要更改任何内容或提供更多信息,请告诉我们!

1 个答案:

答案 0 :(得分:0)

min_params(x, *params)例程使用要优化的参数向量调用函数。因此,您的函数称为*params,其中args是您使用关键字参数x提供给函数的函数的自定义参数。定义函数params的方式最终将作为函数内max_pool的第一个元素。

假设r_probtau_recovdef min_params(params): ... y = optimize.brute(min_params, rranges) 是你想要优化的,那么就是如何解决问题:

{{1}}