不清楚的索引错误

2024-04-26 04:09:04 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在StackOverflow中的第三个线程。 我想我已经通过阅读这里的线索和消除我的疑虑学到了很多。你知道吗

我正在尝试用我自己的python脚本转换excel表。我做了这么多,现在我几乎完成了脚本,我得到了一个错误的信息,我不能真正理解。这是我的代码:(我试着提供尽可能多的信息!)你知道吗

def _sensitivity_analysis(datasource):
    #datasource is a list with data that may be used for HBV_model() function;
    datasource_length = len(datasource) #returns tha size of the data time series
    sense_param = parameter_vector #collects the parameter data from the global vector (parameter_vector);
    sense_index = np.linspace(0, 11, 12) #Vector that reflects the indexes of parameters that must be analyzed (0 - 11)
    sense_factor = np.linspace(0.5, 2, 31) #Vecor with the variance factors that multiply the original parameter value;
    ns_sense = [] #list that will be filled with Nasch-Sutcliff values (those numbers will be data for sensitivity analysis)
    for i in range(sense_factor.shape[0]): #start column loop
        ns_sense.append([]) #create column in ns_sense matrix
        for j in range(sense_index.shape[0]): #start row loop
            aux = sense_factor[i]*sense_param[j] #Multiplies the param[j] value by the factor[i] value
            print(i,j,aux) #debug purposes
            sense_param[j] = aux #substitutes the original parameter value by the modified one
            hbv = _HBV_model(datasource, sense_param) #run the model calculations (works awesomely!)
            sqrdiff = _square_diff() #does square-difference calculations for Nasch-Sutcliff;
            average = _qcalc_qmed() #does square-difference calculations for Nasch-Sutcliff [2];
            nasch = _nasch_sutcliff(sqrdiff, average) #Returns the Nasch-Sutcliff calculation value
            ns_sense[i].insert(j, nasch) #insert the value into ns_sense(i, j) for further uses;
            sense_param = np.array([np.float64(catchment_area), np.float64(thresh_temp), 
                             np.float64(degreeday_factor), np.float64(field_capacity), 
                                       np.float64(shape_coeficient), np.float64(model_paramC), 
                                       np.float64(surfaceflow_param), np.float64(thresh_surface_level), 
                                       np.float64(interflow_param), np.float64(baseflow_param), 
                                       np.float64(percolation_param), np.float64(soilmoist_param)]) #restores sense_param to original values
            for i in range(len(datasource)): #HBV_model() transforms original data (index = 5) in a fully calculated data (index 17)
                for j in range(12): #in order to return it to original state before a new loop
                    datasource[i].pop() #data is popped out;
    print(ns_sense) #debug purposes

因此,当我运行灵敏度分析(数据源)时,我收到以下消息:

 File "<ipython-input-47-c9748eaba818>", line 4, in <module>
    aux = sense_factor[i]*sense_param[j]
IndexError: index 3652 is out of bounds for axis 0 with size 31; 

我完全知道这是在谈论一个索引,它是不可访问的,因为它不存在。你知道吗

为了说明我的情况,datasource是一个索引为[3652]的列表。但我看不出控制台是如何尝试访问索引3652的,因为我并没有要求它这样做。我尝试访问此类值的唯一一点是在最后一个循环中:

for i in range(len(datasource)):

我真的迷路了。如果你们能帮我,我真的很感激!如果你需要更多的信息,我可以给你。你知道吗


Tags: theinfordatamodelthatparameterparam
2条回答

猜猜:sense_factor = np.linspace(0.5, 2, 31)有31个元素-你要求元素3652,它自然会爆炸。i在最后一个循环中获取这个值。将最终循环重写为:

        for k in range(len(datasource))
            for m in range(12):
                datasource[k].pop()

但是,您的代码有很多问题-您不应该使用索引 根本-而是直接在数组上使用for循环

您重新使用了变量名,如下所示:

for i in range(sense_factor.shape[0]):
    ...
    for j in range(sense_index.shape[0]):

然后在这里:

for i in range(len(datasource)):
    for j in range(12):

所以在aux = sense_factor[i]*sense_param[j]中,您使用了错误的i值,基本上是侥幸没有使用错误的j值。你知道吗

不要在同一范围内重用变量名。你知道吗

相关问题 更多 >