为什么 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 对于某些数组没有返回任何值或返回错误的结果? 的相关文章

  • 为什么我不能使用“exclude”从 python 轮子中排除“tests”目录?

    考虑以下包结构 与以下setup py内容 from setuptools import setup find packages setup name dfl client packages find packages exclude te
  • Python Nose 导入错误

    我似乎无法理解鼻子测试框架 https nose readthedocs org en latest 识别文件结构中测试脚本下方的模块 我已经设置了演示该问题的最简单的示例 下面我会解释一下 这是包文件结构 init py foo py t
  • Numpy 沿轴最大值

    我在这里错过了什么吗 我希望np max在下面的代码片段中将返回 0 4 gt gt gt a array 1 2 0 4 gt gt gt np max a axis 0 array 1 4 感谢您的指点 看起来您想要包含最大值的行 对吧
  • 编辑 scikit-learn 决策树

    我想编辑 sklearn DecisionTree 例如改变条件或切割节点 叶子等 但似乎没有功能可以做到这一点 如果我可以导出到文件 编辑它以导入 如何编辑决策树 环境 Windows 10 python3 3 sklearn 0 17
  • Python:记录垃圾收集器

    我有一个 python 应用程序 有一些性能问题 我想将垃圾收集器的事件 特别是何时调用 添加到我的日志中 是否可以 thanks http docs python org library gc html gc set debug http
  • 为什么 tkinter / window.update 在我的程序中随着时间的推移变得更慢?

    我发现当我调用 window update 时 当向窗口写入的内容较少时 它的运行速度会更快 但后来 当我向窗口写入更多元素时 window update 需要更长的时间 请参阅下面的我的代码 您可以看到它在更新窗口之前一次向屏幕 100
  • 如何在 ReportLab 段落中插入回车符?

    有没有办法在 ReportLab 的段落中插入回车符 我试图将 n 连接到我的段落字符串 但这不起作用 Title Paragraph Title n Page myStyle 我想要这样做 因为我将名称放入单元格中 并且想要控制单元格中的
  • 为什么我的scoped_session 引发 AttributeError: 'Session' object has no attribute 'remove'

    我正在尝试建立一个系统 将数据库操作优雅地推迟到单独的线程 以避免在 Twisted 回调期间发生阻塞 到目前为止 这是我的方法 from contextlib import contextmanager from sqlalchemy i
  • Python 正则表达式部分匹配或“hitEnd”

    我正在编写一个扫描器 因此我将任意字符串与正则表达式规则列表进行匹配 如果我可以模拟 Java hitEnd 功能 不仅知道正则表达式何时不匹配 还知道何时匹配 这将非常有用 can t匹配 当正则表达式匹配器在决定拒绝输入之前到达输入末尾
  • Django 的 URL 覆盖率测试为 0%,为什么?

    使用姜戈鼻子 我对 URL 进行了测试 但 URL 覆盖率仍然为 0 为什么 python manage py 测试配置文件 这是我的报道 Name Stmts Miss Cover Missing profiles 0 0 100 pro
  • 如何使用循环将十进制转换为二进制?

    我想编写一个程序 将十进制数 0 到 9 转换为二进制数 我可以编写如何使用重复除法将十进制数转换为二进制数的代码 但是 我在创建一个以二进制格式打印十进制数字 0 到 9 的循环时遇到了麻烦 这是我的代码 number 0 remaind
  • 如何使用 Pandas 将巨大的 CSV 转换为 SQLite?

    我有一个巨大的表 大约 60 GB 采用存档的 CSV 文件形式 我想将其转换为 SQLite 文件 我现在所做的事情如下 import pandas import sqlite3 cnx sqlite3 connect db sqlite
  • 如何使用 PySpark 有效地将这么多 csv 文件(大约 130,000 个)合并到一个大型数据集中?

    我之前发布了这个问题并得到了一些使用 PySpark 的建议 如何有效地将这一大数据集合并到一个大数据框中 https stackoverflow com questions 60259271 how can i merge this la
  • Python 视频框架

    我正在寻找一个 Python 框架 它将使我能够播放视频并在该视频上绘图 用于标记目的 我尝试过 Pyglet 但这似乎效果不是特别好 在现有视频上绘图时 会出现闪烁 即使使用双缓冲和所有这些好东西 而且似乎没有办法在每帧回调期间获取视频中
  • 如何检查列表是否为空?

    这个问题的答案是社区努力 help privileges edit community wiki 编辑现有答案以改进这篇文章 目前不接受新的答案或互动 例如 如果通过以下内容 a 我如何检查是否a是空的 if not a print Lis
  • Spark中的count和collect函数抛出IllegalArgumentException

    当我使用时抛出此异常时 我尝试在本地 Spark 上加载一个小数据集count 在 PySpark 中 take 似乎有效 我试图搜索这个问题 但没有找到原因 看来RDD的分区有问题 有任何想法吗 先感谢您 sc stop sc Spark
  • 如何强制 Y 轴仅使用整数

    我正在使用 matplotlib pyplot 模块绘制直方图 我想知道如何强制 y 轴标签仅显示整数 例如 0 1 2 3 等 而不显示小数 例如 0 0 5 1 1 5 2 等 我正在查看指导说明并怀疑答案就在附近matplotlib
  • 为什么 bot.get_channel() 会产生 NoneType?

    我正在制作一个 Discord 机器人来处理公告命令 当使用该命令时 我希望机器人在特定通道中发送一条消息 并向用户发送一条消息以表明该命令已发送 但是 我无法将消息发送到频道 我尝试了这段代码 import discord import
  • 最小硬币找零问题——回溯

    我正在尝试用最少数量的硬币解决硬币找零问题 采用回溯法 我实际上已经完成了它 但我想添加一些选项 按其单位打印硬币数量 而不仅仅是总数 这是我下面的Python代码 def minimum coins coin list change mi
  • issubclass() 对从不同路径导入的同一类返回 False

    目的是实现某种插件框架 其中插件是同一基类 即 A 的子类 即 B 基类使用标准导入加载 而子类使用 imp load module 从众所周知的包 即 pkg 的路径加载 pkg init py mod1 py class A mod2

随机推荐