Home > database >  Numpy Attribute Error : Float object has no attribtue 'arctan2'
Numpy Attribute Error : Float object has no attribtue 'arctan2'

Time:02-12

I have been trying to implement this piece of code but since I am very new to numpy I have less idea about this error.

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

Which gives me an error:

    elevation = np.arctan2(z,np.sqrt(x**2   y**2))
AttributeError: 'Float' object has no attribute 'arctan2'

I am already passing the value to the function as float. Even the importing numpy works fine.

CodePudding user response:

I added a print statement right before the arctan:

def asSpherical(coord):
    x=coord[0]
    y=coord[1]
    z=coord[2]
    print(x,y,z)
    azimuth = np.arctan2(y,x)
    ...

and got this run:

....
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 is -37.062720709188 - 45.0*sin(180), where sin(180) a sympy expression.

I see sin(phi) in the trans_matrix expression. Because of the from sympy import *, sin (as opposed to np.sin) is a sympy function.

Removing that import (as I commented) raises:

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

Changing that to np.sin (like the other functions in the definition), lets the script run without error.

  • Related