首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Numpy属性错误:浮点对象没有attribtue 'arctan2‘

Numpy属性错误:浮点对象没有attribtue 'arctan2‘
EN

Stack Overflow用户
提问于 2022-02-11 02:59:07
回答 1查看 259关注 0票数 0

我一直在尝试实现这段代码,但是由于我对numpy非常陌生,所以我对这个错误知之甚少。

代码语言:javascript
运行
AI代码解释
复制
import numpy as np
from sympy import *
import matplotlib.pyplot as plt

def asSpherical(coord):
    x=coord[0]
    y=coord[1]
    z=coord[2]
    azimuth = np.arctan2(y,x)
    elevation = np.arctan2(z,np.sqrt(x**2 + y**2))
    r = np.sqrt(x**2 + y**2 + z**2)
    return [azimuth, elevation, r]

def ecefToEnu(lamb, phi, coord_set):
    # @param1 is lattitude
    # @param2 is longitude
    # @param3 is coordinate set in form of a list

    # R matrix of size 3x3
    # This transforms the ECEF coordinates to ENU
    trans_matrix = [
        [-np.sin(lamb), np.cos(lamb), 0],
        [-(np.cos(lamb))*np.sin(phi), -
         np.sin(lamb)*np.sin(phi), np.cos(phi)],
        [np.cos(lamb)*np.cos(phi), np.sin(lamb)*np.cos(phi), sin(phi)]
    ]
    # Performs the matrix multiplication
    enu_coords = np.matmul(trans_matrix, coord_set)
    # Returns a list of Cartesian Coordinates
    sph_coords = asSpherical(enu_coords)
    return sph_coords


coord_list = []
print("Enter the latitude")
latitude = float(input())
print("Enter the coordinate list:")
print("Enter X:")
coord_list.append(float(input()))
print("Enter Y:")
coord_list.append(float(input()))
print("Enter Z:")
coord_list.append(float(input()))
phiDegSet = np.arange(-180, 180, 1)
Nset = len(phiDegSet)
t0 = np.linspace(0, 24, Nset)
t0 = list(t0)

res = []
for i in range(0, 360):
    temp_list = ecefToEnu(latitude, phiDegSet[i], coord_list)
    res.append(temp_list)
elevDegSet = []
for i in res:
    elevDegSet.append(i[1])

这给了我一个错误:

代码语言:javascript
运行
AI代码解释
复制
    elevation = np.arctan2(z,np.sqrt(x**2 + y**2))
AttributeError: 'Float' object has no attribute 'arctan2'

我已经将该值作为float传递给该函数。即使是进口的numpy也能正常工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-11 08:51:59

我在print之前添加了一个arctan语句

代码语言:javascript
运行
AI代码解释
复制
def asSpherical(coord):
    x=coord[0]
    y=coord[1]
    z=coord[2]
    print(x,y,z)
    azimuth = np.arctan2(y,x)
    ...

得到了这个结果:

代码语言:javascript
运行
AI代码解释
复制
....
ter the latitude
45
Enter the coordinate list:
Enter X:
45
Enter Y:
45
Enter Z:
45
-14.65116910723749 -76.54620448997258 -37.062720709188 - 45.0*sin(180)
Traceback (most recent call last):
  File "stack71078990.py", line 52, in <module>
    temp_list = ecefToEnu(latitude, phiDegSet[i], coord_list)
  File "stack71078990.py", line 31, in ecefToEnu
    sph_coords = asSpherical(enu_coords)
  File "stack71078990.py", line 11, in asSpherical
    elevation = np.arctan2(z,np.sqrt(x**2 + y**2))
AttributeError: 'Add' object has no attribute 'arctan2'

z-37.062720709188 - 45.0*sin(180),其中sin(180)sympy表达式。

我在sin(phi)表达式中看到了trans_matrix。由于from sympy import *sin (而不是np.sin)是sympy函数。

删除该导入(正如我评论的那样)会引发:

代码语言:javascript
运行
AI代码解释
复制
Traceback (most recent call last):
  File "stack71078990.py", line 52, in <module>
    temp_list = ecefToEnu(latitude, phiDegSet[i], coord_list)
  File "stack71078990.py", line 26, in ecefToEnu
    [np.cos(lamb)*np.cos(phi), np.sin(lamb)*np.cos(phi), sin(phi)]
NameError: name 'sin' is not defined

将其更改为np.sin (与定义中的其他函数一样),可以让脚本运行而不会出错。

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

https://stackoverflow.com/questions/71078990

复制
相关文章

相似问题

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