1
>>> arr = np.array([[3, -4, 4], [1, -2, 2]])
>>> arr
array([[ 3, -4,  4],
       [ 1, -2,  2]])
>>> arr[1]-(1/3)*arr[0]
array([ 0.        , -0.66666667,  0.66666667])
>>> arr[1] = arr[1]-(1/3)*arr[0]
>>> arr
array([[ 3, -4,  4],
       [ 0,  0,  0]])

what do I do wrong? I want to assign the result of the calculation to the second row of the array "arr"

2
  • You can't assign back because its an int array, doesn't support float pt data. Commented Jun 26, 2017 at 14:18
  • The array is an int array. Commented Jun 26, 2017 at 14:18

1 Answer 1

2

The problem is that you constructed an array of ints. You can construct an array of np.floats by using the dtype parameter:

arr = np.array([[3, -4, 4], [1, -2, 2]],dtype=np.float)

Python has a dynamic approach when it comes to types: every element in a list can have a different type. But numpy works with matrices where all elements have the same type. Therefore assigning a float to an int matrix, will convert the row first to ints.

This will construct an array:

>>> arr = np.array([[3, -4, 4], [1, -2, 2]],dtype=np.float)
>>> arr[1] = arr[1]-(1/3)*arr[0]
>>> arr
array([[ 3.        , -4.        ,  4.        ],
       [ 0.        , -0.66666667,  0.66666667]])
Sign up to request clarification or add additional context in comments.

Comments

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.