user288609
user288609

Reputation: 13035

regarding unboundlocalerror: local variable 'K' referenced before assignment

I have been trying to experiment with the geostatistics library`, one of its file has the following function,

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

It seems to me that it contains two possible functions:, simple and ordinary, through lambda. Does u after lambda correspond to grid[i] in est[i], kstd[i] = k( data, covfct, grid[i], N, nugget )?

I run the example code given here, given here. For instance,

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)

It gives the following error message, I do not understand what does it mean?

enter image description here

Upvotes: 0

Views: 345

Answers (1)

Rahul
Rahul

Reputation: 11560

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 )

If any of conditions are not met, k never exists so it gives the UnboundLocalError.

kv = kriging.krige(data,kriging.spherical,hs,bw,u,N) here you are passing bw = 0.5 which is neither 'simple' nor 'ordinary'. you need to pass required argument to function.

Upvotes: 3

Related Questions