Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'numpy.float64' object is not iterable

Tags:

I'm trying to iterate an array of values generated with numpy.linspace:

slX = numpy.linspace(obsvX, flightX, numSPts) slY = np.linspace(obsvY, flightY, numSPts)  for index,point in slX:     yPoint = slY[index]     arcpy.AddMessage(yPoint) 

This code worked fine on my office computer, but I sat down this morning to work from home on a different machine and this error came up:

File "C:\temp\gssm_arcpy.1.0.3.py", line 147, in AnalyzeSightLine   for index,point in slX: TypeError: 'numpy.float64' object is not iterable 

slX is just an array of floats, and the script has no problem printing the contents -- just, apparently iterating through them. Any suggestions for what is causing it to break, and possible fixes?

like image 974
Erica Avatar asked May 31 '13 13:05

Erica


People also ask

How can I check if numpy float64 is NaN?

To check for NaN values in a Numpy array you can use the np. isnan() method. This outputs a boolean mask of the size that of the original array. The output array has true for the indices which are NaNs in the original array and false for the rest.

What is the difference between float and float64?

float is an alias for python float type. np. float32 and np. float64 are numpy specific 32 and 64-bit float types.

What does float object is not iterable mean?

Conclusion # The Python "TypeError: 'float' object is not iterable" occurs when we try to iterate over a float or pass a float to a built-in function like, list() or tuple() . To solve the error, use the range() built-in function to iterate over a range, e.g. for i in range(int(3.0)): .

Does Python use float64?

Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to np. float64 . In some unusual situations it may be useful to use floating-point numbers with more precision.


1 Answers

numpy.linspace() gives you a one-dimensional NumPy array. For example:

>>> my_array = numpy.linspace(1, 10, 10) >>> my_array array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.]) 

Therefore:

for index,point in my_array 

cannot work. You would need some kind of two-dimensional array with two elements in the second dimension:

>>> two_d = numpy.array([[1, 2], [4, 5]]) >>> two_d array([[1, 2], [4, 5]]) 

Now you can do this:

>>> for x, y in two_d:     print(x, y)  1 2 4 5 
like image 111
Mike Müller Avatar answered Dec 11 '22 04:12

Mike Müller