python - 在 scikit 中使用高斯过程的拟合函数中出现类型错误

标签 python numpy scipy scikit-learn interpolation

好的,所以我一直在编写一些代码,这些代码获取表示来自 Houdini 的稀疏点数据的图像,并将其插值到可用的完整 map 中。这一直运行得很好,只是现在我遇到了一些内存问题。我已经将我使用的克里金算法中的内存问题缩小到了 Predict() 步骤。我试图使用batch_size参数来限制内存消耗,但这很痛苦。我收到此错误:

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参数的类型,它是一个int,而不是一个float。我真的需要这个工作,这样我就可以得到一个输出,用于我的硕士学位的最终项目,该项目将在几周后到期。我包括下面的代码。另外,如果有人对如何提高径向计算的内存效率有任何建议,我非常乐意。

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")

最佳答案

这看起来像是 python 3 中 scikit-learn 中的一个错误 - 划分 here在 python 3 中产生一个 float ,range 然后正确地拒绝。

有一个corresponding issue here ,但它似乎是一个 wontfix,引用 GaussianProcess 无论如何已被弃用

关于python - 在 scikit 中使用高斯过程的拟合函数中出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37304309/

相关文章:

python - Python如何按降序排列每个字母的出现频率?

python - 将 Python 更新到 3.8,模块不起作用

python - 在 Python 中编译正则表达式

c# - 在锁定的 Windows 10 机器上的后台进程中将文本放入剪贴板

Python 新手 : manipulating arrays

python - Sympy:lambda 化使得对数组的操作总是产生数组,对于常量也是如此?

python - 无法在 matplotlib 图中出现次要网格线

python - 多元学生 t 分布与 python

python - `scipy.optimize.root` 更快的根查找

python - 网页抓取器返回元素列表