0

So I'm currently learning python and numpy and I'm testing this code because I can't get A[:, 1] = arr[:, 0] to work whatsoever. I thought it was maybe an object thing or something (coming from javascript/java/C to python) but it seems it's actually the arr[:, 0] that is causing issues saying that there is too many indices... but I'm just printing all of the indices in column 1 right? Lost Here.

import numpy as np

arr = np.array([1, 2, 3, 4])
degree = 7
A = np.ones((len(arr), degree))
print(A[:, 1])
print(arr[:, 0])

^ full attached code above

1
  • 1
    What is the expected output? arr is one dimensional so you can't give it two dimensional indices. Commented Apr 14, 2022 at 1:05

1 Answer 1

1

For a numpy array, the number of indices cannot exceed the number of dimensions. Thus for a 1D array, giving two indices (:, 0 is two indices) is invalid, which is why you observe this error for arr but not for A.

arr[0] would give you the first element of arr, or just arr gives you the entire array.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.