首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在scikit中使用高斯过程的我的拟合函数的类型错误

在scikit中使用高斯过程的我的拟合函数的类型错误
EN

Stack Overflow用户
提问于 2016-05-18 23:41:03
回答 1查看 285关注 0票数 2

好的,我一直在编写一些代码,这些代码从胡迪尼获取代表稀疏点数据的图像,并将其插值到可用的完整地图中。这一直运行得很好,除了现在我遇到了一些内存问题。我已经缩小了我用来预测()步骤的克里格算法中的内存问题。我尝试使用batch_size参数来限制内存消耗,但这很麻烦。我得到了这个错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "e:\mapInterpolation.py", line 88, in <module>
    prepareImage(file, interpType="kriging")
  File "e:\mapInterpolation.py", line 61, in prepareImage
    rInterpInt = kriging(r).astype(int)
  File "e:\mapInterpolation.py", line 36, in kriging
    interpolated = gp.predict(rr_cc_as_cols, batch_size=a).reshape(data.shape)
  File "e:\miniconda\lib\site-packages\sklearn\gaussian_process\gaussian_process
.py", line 522, in predict
    for k in range(max(1, n_eval / batch_size)):
TypeError: 'float' object cannot be interpreted as an integer

我已经对传递给batch_size参数的类型进行了三次检查,它是一个整型,而不是浮点型。我真的需要这样做,这样我就可以在几周后完成的硕士期末项目中得到一份输出。我包含了下面的代码。此外,如果任何人有任何关于如何使径向计算更有效的内存的建议,我非常乐意。

代码语言:javascript
复制
import numpy as np
def parseToM(array):
    print("parsing to matrix")
    r = np.linspace(0, 1, array.shape[0]) 
    c = np.linspace(0, 1, array.shape[1])    
    rr, cc = np.meshgrid(r, c)
    vals = ~np.isnan(array)
    return {"rr":rr, "cc":cc, "vals":vals}

def radial(data):
    import scipy.interpolate as interpolate
    hold = parseToM(data)
    rr, cc, vals = hold["rr"], hold["cc"], hold["vals"]    

    print("starting RBF interpolation")
    f = interpolate.Rbf(rr[vals], cc[vals], data[vals], function='linear')
    print("storing data")
    interpolated = f(rr, cc)
    return interpolated

def kriging(data):
    from sklearn.gaussian_process import GaussianProcess

    hold = parseToM(data)   
    rr, cc, vals = hold["rr"], hold["cc"], hold["vals"]

    print("starting gaussian process")
    gp = GaussianProcess(theta0=0.1, thetaL=.001, thetaU=1., nugget=0.1, storage_mode="light")
    print("fitting data")
    gp.fit(X=np.column_stack([rr[vals],cc[vals]]), y=data[vals])
    print("flattening data")
    rr_cc_as_cols = np.column_stack([rr.flatten(), cc.flatten()])
    print("reshaping data")
    a = 1000
    print(type(a))
    interpolated = gp.predict(rr_cc_as_cols, batch_size=a).reshape(data.shape)
    return interpolated

def prepareImage(filename, interpType="kriging"):
    print("opening image", filename)
    from PIL import Image
    f = Image.open(filename)
    image = f.load()
    image_size = f.size
    xmax = image_size[0]
    ymax = image_size[1]
    r = np.ndarray(shape=(xmax, ymax))
    g = np.ndarray(shape=(xmax, ymax))
    b = np.ndarray(shape=(xmax, ymax))
    print("processing image")
    for x in range(xmax):
        for y in range(ymax):
            value = image[x,y]
            if value[3] == 0:
                r[x,y], g[x,y], b[x,y] = [np.nan, np.nan, np.nan]
            else:
                r[x,y], g[x,y], b[x,y] = value[:3]

    print("interpolating")
    if interpType == "kriging":
        rInterpInt = kriging(r).astype(int)
        gInterpInt = kriging(g).astype(int)
        bInterpInt = kriging(b).astype(int)
    elif interpType == "radial":
        rInterpInt = radial(r).astype(int)
        gInterpInt = radial(g).astype(int)
        bInterpInt = radial(b).astype(int)

    print("reapplying pixels")
    for i in range(rInterpInt.size):
        if rInterpInt.item(i) < 0:
            rInterpInt.itemset(i, 0)
        if gInterpInt.item(i) < 0:
            gInterpInt.itemset(i, 0)
        if bInterpInt.item(i) < 0:
            bInterpInt.itemset(i, 0)
        x = i%xmax
        y = int(np.floor(i/ymax))
        newValue = (rInterpInt[x,y], gInterpInt[x,y], bInterpInt[x,y], 255)
        image[x,y] = newValue
    print("saving")
    savename = "E:\\"+filename[3:9]+"."+interpType+".png"
    f.save(savename, "PNG")
    print("done")

for i in range(1,10):
    file = r"E:\occ"+str(i*100)+".png"
    prepareImage(file, interpType="kriging")
EN

回答 1

Stack Overflow用户

发布于 2016-05-19 00:16:52

这看起来像是range中的一个bug --在python3中学习--除法here导致了python3中的一个浮点数,然后range正确地拒绝了它。

有一个corresponding issue here,但它似乎是一个wontfix,因为GaussianProcess无论如何都会被弃用

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37304309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档