关于unboundlocalerror:在赋值之前引用的局部变量'K'

时间:2017-07-15 04:04:19

标签: python python-3.x

我一直试图尝试geostatistics library`,其中一个文件具有以下功能,

def krige( data, covfct, grid, method='simple', N=0, nugget=0 ):
    '''
    Krige an <Nx2> array of points representing a grid.

    Use either simple or ordinary kriging, some number N
    of neighboring points, and a nugget value.
    '''
    if method == 'simple':
        k = lambda d, c, u, N, nug: simple( d, c, u, N, nug ) 
    elif method == 'ordinary':
        k = lambda d, c, u, N, nug: ordinary( d, c, u, N, nug )
    print('method = ',method)
    M = len( grid )
    est, kstd = np.zeros(( M, 1 )), np.zeros(( M, 1 ))
    for i in range( M ):
        est[i], kstd[i] = k( data, covfct, grid[i], N, nugget )
    return est, kstd

在我看来,它包含两个可能的功能:simpleordinary,通过lambdau之后的lambdagrid[i]中的est[i], kstd[i] = k( data, covfct, grid[i], N, nugget )相对应吗?

我运行此处给出的示例代码given here。例如,

import unittest
from geostatsmodels import kriging 
import numpy as np

data = np.array([[0,0,1],[1,0,3],[0,1,3],[1,1,4]])
hs = np.array([0.5,1.0,1.5,2.0])
bw = 0.5
u = [0.5,0.5]
N = 2
eps = 0.0001
kv = kriging.krige(data,kriging.spherical,hs,bw,u,N)

它给出了以下错误信息,我不明白这是什么意思?

enter image description here

1 个答案:

答案 0 :(得分:3)

if method == 'simple':
    k = lambda d, c, u, N, nug: simple( d, c, u, N, nug ) 
elif method == 'ordinary':
    k = lambda d, c, u, N, nug: ordinary( d, c, u, N, nug )

如果不满足任何条件,则k永远不存在,因此它给出UnboundLocalError

kv = kriging.krige(data,kriging.spherical,hs,bw,u,N) 在这里,您传递的bw = 0.5既不是'simple'也不是'ordinary'。 你需要将所需的参数传递给函数。