3

I defined the following numpy array:

import numpy as np
numpy_array = np.array([[-1,-1,-1,-1,-1], [-1,2,2,2,-1], [-1,2,8,2,-1], [-1,2,2,2,-1], [-1,-1,-1,-1,-1,-1]])

Now I wanted to divide the whole array by 8:

numpy_array /= np.float(8.0)

I get the following error message:

TypeError: unsupported operand type(s) for /: 'list' and 'float'

I hope someone has a hint for me, what I am doing wrong.

3
  • 1
    That doesn't look like a regular array. Each element of it is a list and not an array. Commented May 31, 2017 at 19:36
  • 3
    Is the final element in the list meant to have 6 items, the others have only 5? Commented May 31, 2017 at 19:41
  • @njoose you are right, this is an error! Commented May 31, 2017 at 19:44

2 Answers 2

4

The array has a list of the incorrect size, the final list is incorrect.

if you want to keep the data as an int, you can use numpy_array = np.divide(numpy_array, 8.0), otherwise see MSeifert's answer.

Sign up to request clarification or add additional context in comments.

Comments

3

Unfortunatly your inner lists aren't the same size so numpy creates an object array:

>>> numpy_array
array([[-1, -1, -1, -1, -1], [-1, 2, 2, 2, -1], [-1, 2, 8, 2, -1],
       [-1, 2, 2, 2, -1], [-1, -1, -1, -1, -1, -1]], dtype=object)

You should try to avoid object arrays because they might not behave like expected and of course they are slower. If you need different sized inner lists, you should mask elements:

>>> import numpy as np
>>> numpy_array = np.array([[-1,-1,-1,-1,-1, np.nan], 
...                         [-1, 2, 2, 2,-1, np.nan], 
...                         [-1, 2, 8, 2,-1, np.nan], 
...                         [-1, 2, 2, 2,-1, np.nan], 
...                         [-1,-1,-1,-1,-1,-1]])
>>> numpy_array = np.ma.array(numpy_array, mask=np.isnan(numpy_array))
>>> numpy_array /= 8.0
>>> numpy_array
masked_array(data =
 [[-0.125 -0.125 -0.125 -0.125 -0.125 --]
 [-0.125 0.25 0.25 0.25 -0.125 --]
 [-0.125 0.25 1.0 0.25 -0.125 --]
 [-0.125 0.25 0.25 0.25 -0.125 --]
 [-0.125 -0.125 -0.125 -0.125 -0.125 -0.125]],
             mask =
 [[False False False False False  True]
 [False False False False False  True]
 [False False False False False  True]
 [False False False False False  True]
 [False False False False False False]],
       fill_value = 1e+20)

Also you need to be careful with inplace-operations because they don't change the dtype, so when you have an integer array and in-place-divide it with a float you'll still have an integer array (with truncated results) or get an exception:

>>> arr = np.array([1, 2, 3])
>>> arr /= 8.0
TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''

Create a float array instead (or cast to float or just do a normal division arr = arr / 8.0):

>>> arr = np.array([1, 2, 3], dtype=float)
>>> arr /= 8.0
>>> arr
array([ 0.125,  0.25 ,  0.375])

2 Comments

Thank you very much for this detailed answer! Actually I made an mistake, the last list had 6 elements, but should have had only 5 (Thanks to njoose for pointing out). Applying your approached worked!
@Max Okay, I thought that was intentional. But if that was just an oversight you only need to remove the unnecessary element from the last list :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.