Selecting first n elemets of a numpy array

Question:

I’m trying to get the first 784 elements of a numpy array. Is there a way to this? I tried doing array = array[:783], but that doesn’t seem to work. I also tried converting it to a list and then back to a numpy array like this

python_list = numpy_array.tolist()
cut_numpy_array = np.array(python_list[:783])

but that also doesn’t seem to work. Is there a way to do this?

Asked By: Jan Hrubec

||

Answers:

If you have more than one-dimensional numpy array, slicing won’t work that way. You can try other ways.

Use np.ravel()

array = np.ravel(array)[0:783]
Answered By: vignesh kanakavalli

If you are working with 2D array then you have to give starting and ending point of rows and starting and ending point of columns.
value = image[starting:ending, starting:ending]

Answered By: Rehman Ali
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.