查找类型错误减去两个相同大小和类型的 numpy.ndarrays

发布于 2024-12-18 14:41:20 字数 2333 浏览 0 评论 0 原文

我试图通过从罗盘航向中减去太阳方位角来找到传感器与太阳的相对位置。出于测试目的,我有两个 numpy 向量。一份带有 52 个不同样本的太阳方位角,另一份带有 52 个不同样本的指南针。

我一直在 numpy 向量上执行这种操作数,所以我不知道为什么它会给我以下错误。

    TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'

这是一些代码,我将尝试尽可能多地包含,但它来自较大的代码库。

    ####STUFF above####

    # for debugging
    a = dalecData.get_solar_azimuth()
    b = dalecData.get_cmp_heading()

    print type(a)
    print type(b)

    print a.shape
    print b.shape

    print a.flags
    print b.flags

    print a.ndim
    print b.ndim

    a - b

    #work aroundS ????

    #sensorAzimuth = subract(dalecData.get_solar_azimuth(),dalecData.get_cmp_heading()) 
    #sensorAzimuth = asarray(dalecData.get_solar_azimuth()) -  asarray(dalecData.get_cmp_heading())## wtf?
    #for i in range(0,dalecData.get_solar_azimuth().shape[0]):
    #    sensorAzimuth[i] = dalecData.get_solar_azimuth()[i] - dalecData.get_cmp_heading()[i]

给我以下输出

    <type 'numpy.ndarray'>
    <type 'numpy.ndarray'>
    (52, 1)
    (52, 1)
    C_CONTIGUOUS : True
    F_CONTIGUOUS : False
    OWNDATA : True
    WRITEABLE : True
    ALIGNED : True
    UPDATEIFCOPY : False
    C_CONTIGUOUS : True
    F_CONTIGUOUS : False
    OWNDATA : True
    WRITEABLE : True
    ALIGNED : True
    UPDATEIFCOPY : False
    2
    2

所有“解决方法”都给出相同的错误

初始化向量

    # viewing geometry
    self.__solarZenith = zeros((1))
    self.__solarAzimuth = zeros((1))

使用访问器

    def get_solar_zenith(self):
        return self.__solarZenith


    def get_solar_azimuth(self):
        return self.__solarAzimuth

    def set_cmp_heading(self,value):
        self.__cmpHeading = value

并使用填充

    row = self.findClosestDatetime(self.__edStartTime[i], self.__cmpDateTime)
    heading = vstack((heading, self.__cmpHeading[row-1]))
    heading = delete(heading,0,0) # get rid of initialised 0
    self.set_cmp_heading(heading)

这应该将数据重新采样到 edStartTime 采样时间。并且似乎有效。

太阳天顶和方位角是使用 pysolar 计算的 http://pysolar.org/

所有值似乎都是正确的,我只是无法将这些值相减。它只发生在这些向量上。我可以在代码中的其他地方以相同的方式添加和减去其他向量。

我希望我已经解释了我的情况。我希望能帮助您理解该错误。

谢谢

I am trying to find my sensor's relative position to the sun by subtracting the solar azimuth from the compass heading. For testing purposes I have two numpy vectors. One with the solar azimuth for 52 different samples and one with the compass for 52 different samples.

I do this kind of operand on numpy vectors all the time, so I don't know why it is giving me the following error.

    TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'

This is some of the code, I'll try include as much as possible but it comes from a largish code base.

    ####STUFF above####

    # for debugging
    a = dalecData.get_solar_azimuth()
    b = dalecData.get_cmp_heading()

    print type(a)
    print type(b)

    print a.shape
    print b.shape

    print a.flags
    print b.flags

    print a.ndim
    print b.ndim

    a - b

    #work aroundS ????

    #sensorAzimuth = subract(dalecData.get_solar_azimuth(),dalecData.get_cmp_heading()) 
    #sensorAzimuth = asarray(dalecData.get_solar_azimuth()) -  asarray(dalecData.get_cmp_heading())## wtf?
    #for i in range(0,dalecData.get_solar_azimuth().shape[0]):
    #    sensorAzimuth[i] = dalecData.get_solar_azimuth()[i] - dalecData.get_cmp_heading()[i]

Gives me the following output

    <type 'numpy.ndarray'>
    <type 'numpy.ndarray'>
    (52, 1)
    (52, 1)
    C_CONTIGUOUS : True
    F_CONTIGUOUS : False
    OWNDATA : True
    WRITEABLE : True
    ALIGNED : True
    UPDATEIFCOPY : False
    C_CONTIGUOUS : True
    F_CONTIGUOUS : False
    OWNDATA : True
    WRITEABLE : True
    ALIGNED : True
    UPDATEIFCOPY : False
    2
    2

All of the 'workarounds' give the same error

The vectors are initialised using

    # viewing geometry
    self.__solarZenith = zeros((1))
    self.__solarAzimuth = zeros((1))

Accessors

    def get_solar_zenith(self):
        return self.__solarZenith


    def get_solar_azimuth(self):
        return self.__solarAzimuth

    def set_cmp_heading(self,value):
        self.__cmpHeading = value

And populated using

    row = self.findClosestDatetime(self.__edStartTime[i], self.__cmpDateTime)
    heading = vstack((heading, self.__cmpHeading[row-1]))
    heading = delete(heading,0,0) # get rid of initialised 0
    self.set_cmp_heading(heading)

This should resample the data to the edStartTime sample times. And seems to work.

solar zenith and azimuth are calculated using pysolar http://pysolar.org/

All of the values seem to be correct, I just can't subtract the values from each other. It only happens with these vectors. I can add and subtract other vectors in the same manner elsewhere in my code.

I hope I have explained my situation. I would appreciate help understanding the error.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杯敬自由 2024-12-25 14:41:20

这看起来像你的数组是一个 结构化数组 你可以检查一下通过使用 a.dtypeb.dtype 如果输出看起来像 dtype('float64') 它们我错了,但如果它看起来喜欢[('f0', ' 那么数组的元素是带有浮点数的记录,而不是浮点数本身,并且 numpy 不知道如何操作它们。

例如这个:

>>> x = np.array([1,2,3])
>>> x.dtype
dtype('int64')
>>> np.diff(x)
array([1, 1])
>>> x.dtype = np.dtype([('f0', '<i8')])
>>> np.diff(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 998, in diff
return a[slice1]-a[slice2]
TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'

this looks like your array is an structured array You can check it by using a.dtype and b.dtype if the output looks like dtype('float64') them I am wrong, but if it looks like [('f0', '<f8')] then the elements of your array(s) are records with floats, not floats themselves, and numpy does not know how to operate them.

This for example:

>>> x = np.array([1,2,3])
>>> x.dtype
dtype('int64')
>>> np.diff(x)
array([1, 1])
>>> x.dtype = np.dtype([('f0', '<i8')])
>>> np.diff(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 998, in diff
return a[slice1]-a[slice2]
TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文