简体   繁体   English

使用Scipy.optimization的蛮干功能时,为什么会出现“ ValueError:设置具有序列的数组元素”的问题?

[英]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_poolr_probtau_recov你想在这里过优化是什么如何解决的事情:

def min_params(params):
    ...
y = optimize.brute(min_params, rranges)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:在调用带有x和y args的函数时,scipy.optimize.minimize失败,并显示“ ValueError:设置具有序列的数组元素。” - Python: scipy.optimize.minimize fails with “ValueError: setting an array element with a sequence.” when calling on a function with x and y args scipy.brute ValueError:使用序列设置数组元素 - scipy.brute ValueError: setting an array element with a sequence “ ValueError:使用序列设置数组元素。” TensorFlow - “ValueError: setting an array element with a sequence.” TensorFlow ValueError:使用序列设置数组元素。 熊猫 - ValueError: setting an array element with a sequence. for Pandas ValueError:设置具有序列的数组元素。 从csv读取的数据 - ValueError: setting an array element with a sequence. on data read from csv 收到“ ValueError:使用序列设置数组元素。” - receiving “ValueError: setting an array element with a sequence.” 将来自 dataframe 的样本输入到 ARMA model 并得到“ValueError:设置具有序列的数组元素”。 - Inputing samples from a dataframe into an ARMA model and getting 'ValueError: setting an array element with a sequence.' 将HIPS autograd与numpy.piecewise一起使用时发生密码错误(ValueError:使用序列设置数组元素。) - Cryptic error when using HIPS autograd with numpy.piecewise (ValueError: setting an array element with a sequence.) ValueError:使用序列设置数组元素。 我收到提到的 ODEINT 操作数错误。 你能帮我吗 - ValueError: setting an array element with a sequence. i am receiving mentioned error for ODEINT operand. can you help me with that ValueError:使用序列设置数组元素。 熊猫专栏 - ValueError: setting an array element with a sequence. for pandas column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM