为什么 Scipy 的 ndimage.map_coordinates 对于某些数组没有返回任何值或返回错误的结果?

2024-03-31

代码返回正确的值但并不总是返回值

在下面的代码中,python 返回正确的插值arr_b但不是为了arr_a.

不过,我已经研究这个问题大约一天了,我真的不确定发生了什么。

由于某种原因,对于 arr_a,twoD_interpolate 不断返回 [0],即使我摆弄或弄乱数据和输入。

如何修复我的代码,使其实际上对 arr_a 进行插值并返回正确的结果?

import numpy as np
from scipy.ndimage import map_coordinates


def twoD_interpolate(arr, xmin, xmax, ymin, ymax, x1, y1):
    """
    interpolate in two dimensions with "hard edges"
    """
    ny, nx = arr.shape  # Note the order of ny and xy

    x1 = np.atleast_1d(x1)
    y1 = np.atleast_1d(y1)

    # Mask upper and lower boundaries using @Jamies suggestion
    np.clip(x1, xmin, xmax, out=x1)
    np.clip(y1, ymin, ymax, out=y1)

    # Change coordinates to match your array.
    x1 = (x1 - xmin) * (xmax - xmin) / float(nx - 1)
    y1 = (y1 - ymin) * (ymax - ymin) / float(ny - 1)

    # order=1 is required to return your examples.
    return map_coordinates(arr, np.vstack((y1, x1)), order=1)


# test data
arr_a = np.array([[0.7, 1.7, 2.5, 2.8, 2.9],
                  [1.9, 2.9, 3.7, 4.0, 4.2],
                  [1.4, 2.0, 2.5, 2.7, 3.9],
                  [1.1, 1.3, 1.6, 1.9, 2.0],
                  [0.6, 0.9, 1.1, 1.3, 1.4],
                  [0.6, 0.7, 0.9, 1.1, 1.2],
                  [0.5, 0.7, 0.9, 0.9, 1.1],
                  [0.5, 0.6, 0.7, 0.7, 0.9],
                  [0.5, 0.6, 0.6, 0.6, 0.7]])


arr_b = np.array([[6.4, 5.60, 4.8, 4.15, 3.5, 2.85, 2.2],
                  [5.3, 4.50, 3.7, 3.05, 2.4, 1.75, 1.1],
                  [4.7, 3.85, 3.0, 2.35, 1.7, 1.05, 0.4],
                  [4.2, 3.40, 2.6, 1.95, 1.3, 0.65, 0.0]])



# Test the second array
print twoD_interpolate(arr_b, 0, 6, 9, 12, 4, 11)


# Test first area
print twoD_interpolate(
    arr_a, 0, 500, 0, 2000, 0, 2000)

print arr_a[0]

print twoD_interpolate(
    arr_a_60, 0, 500, 0, 2000, 0, 2000)[0]
print twoD_interpolate(
    arr_a, 20, 100, 100, 1600, 902, 50)
print twoD_interpolate(
    arr_a, 100, 1600, 20, 100, 902, 50)
print twoD_interpolate(
    arr_a, 100, 1600, 20, 100, 50, 902)


## Output
[ 1.7]
[ 0.]
[ 0.7  1.7  2.5  2.8  2.9]
0.0
[ 0.]
[ 0.]
[ 0.]

返回错误值的代码:

arr = np.array([[12.8, 20.0, 23.8, 26.2, 27.4, 28.6],
                [10.0, 13.6, 15.8, 17.4, 18.2, 18.8],
                [5.5, 7.7, 8.7, 9.5, 10.1, 10.3],
                [3.3, 4.7, 5.1, 5.5, 5.7, 6.1]])

twoD_interpolate(arr, 0, 1, 1400, 3200, 0.5, 1684)
# above should return 21 but is returning 3.44

这实际上是我在原来的问题中的错。

如果我们检查它试图插入的位置twoD_interpolate(arr, 0, 1, 1400, 3200, 0.5, 1684)我们得到arr[ 170400, 0.1]作为要查找的值,该值将被剪切mode='nearest' to arr[ -1 , 0.1]。注意我切换了x and y获取数组中出现的位置。

这对应于值的插值arr[-1,0] = 3.3 and arr[-1,1] = 4.7所以插值看起来像3.3 * .9 + 4.7 * .1 = 3.44.

问题随之而来。如果我们采用一个从 50 到 250 的数组:

>>> a=np.arange(50,300,50)
>>> a
array([ 50, 100, 150, 200, 250])
>>> stride=float(a.max()-a.min())/(a.shape[0]-1)
>>> stride
50.0

>>> (75-a.min()) * stride
1250.0   #Not what we want!
>>> (75-a.min()) / stride
0.5      #There we go
>>> (175-a.min()) / stride
2.5      #Looks good

我们可以使用查看这个map_coordinates:

#Input array from the above.
print map_coordinates(arr, np.array([[.5,2.5,1250]]), order=1, mode='nearest')
[ 75 175 250] #First two are correct, last is incorrect.

所以我们真正需要的是(x-xmin) / stride,对于前面的示例,步幅为 1,因此这并不重要。

代码应该是这样的:

def twoD_interpolate(arr, xmin, xmax, ymin, ymax, x1, y1):
    """
    interpolate in two dimensions with "hard edges"
    """
    arr = np.atleast_2d(arr)
    ny, nx = arr.shape  # Note the order of ny and xy

    x1 = np.atleast_1d(x1)
    y1 = np.atleast_1d(y1)

    # Change coordinates to match your array.
    if nx==1:
        x1 = np.zeros_like(x1.shape)
    else:
        x_stride = (xmax-xmin)/float(nx-1)
        x1 = (x1 - xmin) / x_stride

    if ny==1:
        y1 = np.zeros_like(y1.shape)
    else:
        y_stride = (ymax-ymin)/float(ny-1)
        y1 = (y1 - ymin) / y_stride

    # order=1 is required to return your examples and mode=nearest prevents the need of clip.
    return map_coordinates(arr, np.vstack((y1, x1)), order=1, mode='nearest')

请注意,夹子不需要mode='nearest'.

print twoD_interpolate(arr, 0, 1, 1400, 3200, 0.5, 1684)
[ 21.024]

print twoD_interpolate(arr, 0, 1, 1400, 3200, 0, 50000)
[ 3.3]

print twoD_interpolate(arr, 0, 1, 1400, 3200, .5, 50000)
[ 5.3]

检查一维或伪一维数组。将插值x仅维度,除非输入数组具有正确的形状:

arr = np.arange(50,300,50)
print twoD_interpolate(arr, 50, 250, 0, 5, 75, 0)
[75]

arr = np.arange(50,300,50)[None,:]
print twoD_interpolate(arr, 50, 250, 0, 5, 75, 0)
[75]

arr = np.arange(50,300,50)
print twoD_interpolate(arr, 0, 5, 50, 250, 0, 75)
[50] #Still interpolates the `x` dimension.

arr = np.arange(50,300,50)[:,None]
print twoD_interpolate(arr, 0, 5, 50, 250, 0, 75)
[75]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 Scipy 的 ndimage.map_coordinates 对于某些数组没有返回任何值或返回错误的结果? 的相关文章

  • Twisted 的 Deferred 和 JavaScript 中的 Promise 一样吗?

    我开始在一个需要异步编程的项目中使用 Twisted 并且文档非常好 所以我的问题是 Twisted 中的 Deferred 与 Javascript 中的 Promise 相同吗 如果不是 有什么区别 你的问题的答案是Yes and No
  • 递归 lambda 表达式可能吗?

    我正在尝试编写一个调用自身的 lambda 表达式 但我似乎找不到任何语法 或者即使它是可能的 本质上我想将以下函数传输到以下 lambda 表达式中 我意识到这是一个愚蠢的应用程序 它只是添加 但我正在探索可以在 python 中使用 l
  • 补丁 - 为什么相对补丁目标名称不起作用?

    我已经从模块导入了一个类 但是当我尝试修补类名而不使用模块作为前缀时 出现类型错误 TypeError Need a valid target to patch You supplied MyClass 例如 以下代码给出了上述错误 imp
  • Pandas:GroupBy 到 DataFrame

    参考这个关于 groupby 到 dataframe 的非常流行的问题 https stackoverflow com questions 10373660 converting a pandas groupby object to dat
  • 如何检查python xlrd库中的excel文件是否有效

    有什么办法与xlrd库来检查您使用的文件是否是有效的 Excel 文件 我知道还有其他库可以检查文件头 我可以使用文件扩展名检查 但为了多平台性我想知道是否有任何我可以使用的功能xlrd库本身在尝试打开文件时可能会返回类似 false 的内
  • 搜索多个字段

    我想我没有正确理解 django haystack 我有一个包含多个字段的数据模型 我希望搜索其中两个字段 class UserProfile models Model user models ForeignKey User unique
  • 没有名为 StringIO 的模块

    我有Python 3 6 我想从另一个名为 run py 的 python 文件执行名为 operation py 的 python 文件 In operation py I do from cStringIO import StringI
  • pytest:同一接口的不同实现的可重用测试

    想象一下我已经实现了一个名为的实用程序 可能是一个类 Bar在一个模块中foo 并为其编写了以下测试 测试 foo py from foo import Bar as Implementation from pytest import ma
  • 使用 python 绘制正值小提琴图

    我发现小提琴图信息丰富且有用 我使用 python 库 seaborn 然而 当应用于正值时 它们几乎总是在低端显示负值 我发现这确实具有误导性 尤其是在处理现实数据集时 在seaborn的官方文档中https seaborn pydata
  • Geodjango距离查询未检索到正确的结果

    我正在尝试根据地理位置的接近程度来检索一些帖子 正如您在代码中看到的 我正在使用 GeoDjango 并且代码在视图中执行 问题是距离过滤器似乎被完全忽略了 当我检查查询集上的距离时 我得到了预期距离 1m 和 18km 但 18km 的帖
  • 通过索引访问Python字典的元素

    考虑一个像这样的字典 mydict Apple American 16 Mexican 10 Chinese 5 Grapes Arabian 25 Indian 20 例如 我如何访问该字典的特定元素 例如 我想在对 Apple 的第一个
  • Jython 和 SAX 解析器:允许的实体不超过 64000 个?

    我做了一个简单的测试xml saxJython 中的解析器在处理大型 XML 文件 800 MB 时遇到以下错误 Traceback most recent call last File src project xmltools py li
  • Python:IndexError:修改代码后列表索引超出范围

    我的代码应该提供以下格式的输出 我尝试修改代码 但我破坏了它 import pandas as pd from bs4 import BeautifulSoup as bs from selenium import webdriver im
  • 使用“默认”环境变量启动新的子进程

    我正在编写一个构建脚本来解析依赖的共享库 及其共享库等 这些共享库在正常情况下是不存在的PATH环境变量 为了使构建过程正常工作 让编译器找到这些库 PATH已更改为包含这些库的目录 构建过程是这样的 加载器脚本 更改 PATH gt 基于
  • 返回表示每组内最大值的索引的一系列数字位置

    考虑一下这个系列 np random seed 3 1415 s pd Series np random rand 100 pd MultiIndex from product list ABDCE list abcde One Two T
  • 在 keras 中保存和加载权重

    我试图从我训练过的模型中保存和加载权重 我用来保存模型的代码是 TensorBoard log dir output model fit generator image a b gen batch size steps per epoch
  • 从 NumPy 数组到 Mat 的 C++ 转换 (OpenCV)

    我正在围绕 ArUco 增强现实库 基于 OpenCV 编写一个薄包装器 我试图构建的界面非常简单 Python 将图像传递给 C 代码 C 代码检测标记并将其位置和其他信息作为字典元组返回给 Python 但是 我不知道如何在 Pytho
  • 动态过滤 pandas 数据框

    我正在尝试使用三列的阈值来过滤 pandas 数据框 import pandas as pd df pd DataFrame A 6 2 10 5 3 B 2 5 3 2 6 C 5 2 1 8 2 df df loc df A gt 0
  • Django 与谷歌图表

    我试图让谷歌图表显示在我的页面上 但我不知道如何将值从 django 视图传递到 javascript 以便我可以绘制图表 姜戈代码 array Year Sales Expenses 2004 1000 400 2005 1170 460
  • 如何根据第一列创建新列,同时考虑Python Pandas中字母和列表的大小? [复制]

    这个问题在这里已经有答案了 我在 Python Pandas 中有 DataFrame 如下所示 col1 John Simon prd agc Ann White BeN and Ann bad list Ben Wayne 我需要这样做

随机推荐