我使用下面的代码来实现我的模型与一些实验数据的最小平方拟合 . 该程序只是继续运行,似乎我的实现有问题,但我无法搞清楚 . 两个函数中的打印语句(simulateFunc和fit)表示这些语句被调用了几次(准确地说是9),但之后没有响应 .

谁能告诉我我的实施有什么问题?程序运行正常,没有使用minimalsq函数,我可以通过调用带有一些参数的fit函数来获取残差 .

示例数据点:(我的文件中共有100个)0.944444444,2.838888889,5.711111111,2.962962963,0.481481481 0.822222222,2.672222222,5.855555556,3.259259259,0.351851852

#!/usr/bin/python

#Simulate egg-laying in hermaphrodites

import math
import csv
import numpy as np
from scipy.optimize import leastsq

f = open('dataPointsCSV.csv')
csv_f = csv.reader(f)

for row in csv_f : 
    ####### My Model #######
    def simulateFunc(parms) : 
        oocytes = 0 
        sperm = 300
        eggs = []
        total_t = 0
        delta_t = 0.1
        bin_size = 4

        k_o = parms[0]
        k_c = parms[1]
        k_f = parms[2]
        k_s = parms[3]

        while(sperm > 1) : 
            oocytes += delta_t*(k_o-k_c*oocytes)
            new_eggs = delta_t*min(k_f*oocytes,k_s*sperm)
            sperm -= new_eggs
            oocytes -= new_eggs
            index = int(total_t/bin_size)

            if(len(eggs)<=index) :
                eggs.append(new_eggs)
            else :
                eggs[index] += new_eggs

            total_t += delta_t

        for i in range(0,len(eggs)) :
            eggs[i] = eggs[i]/bin_size

        return eggs
    ######## Returns the residuals #########
    def fit(para) :
        newEggs = []
        modelEggs = simulateFunc(para)
        exp = np.array(row)
        expEggs = exp.astype(np.float)

        ########## Get the required points for calculating the residuals from the model data ############
        for i in range(0,len(modelEggs)):           
            if(i == 2 or i == 4 or i == 9 or i == 15 or i == 22):
                newEggs.append(modelEggs[i])

        newEggs = np.array(newEggs)
        res = expEggs - newEggs
        return res

    ######## Parameter Initializations ##########
    parameters = [6,-.02,.0266,.0625]

    x, cov, infodict, mesg, ier = leastsq(fit,parameters, full_output = True, epsfcn = 1.0e-2)
    print x