Questions tagged [numpy-ndarray]

Numpy Ndarray refers to the N-dimensional array type that describes the collection of the same type in the Python library NumPy. Use this tag for questions related to this array type.

Numpy Ndarray refers to the N-dimensional array type that describes the collection of the same type in the Python library NumPy.

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html

3757 questions
797
votes
21 answers

How do I get indices of N maximum values in a NumPy array?

NumPy proposes a way to get the index of the maximum value of an array via np.argmax. I would like a similar thing, but returning the indexes of the N maximum values. For instance, if I have an array, [1, 3, 2, 4, 5], then nargmax(array, n=3) would…
Alexis Métaireau
  • 10,767
  • 5
  • 24
  • 34
661
votes
12 answers

What does -1 mean in numpy reshape?

A 2D array can be reshaped into a 1D array using .reshape(-1). For example: >>> a = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> a.reshape(-1) array([[1, 2, 3, 4, 5, 6, 7, 8]]) Usually, array[-1] means the last element. But what does -1 mean here?
user2262504
  • 7,057
  • 5
  • 18
  • 23
555
votes
7 answers

What are the advantages of NumPy over regular Python lists?

What are the advantages of NumPy over regular Python lists? I have approximately 100 financial markets series, and I am going to create a cube array of 100x100x100 = 1 million cells. I will be regressing (3-variable) each x with each y and z, to…
Thomas Browne
  • 23,824
  • 32
  • 78
  • 121
487
votes
9 answers

What is the purpose of meshgrid in NumPy?

What is the purpose of np.meshgrid? I know it creates some kind of grid of coordinates for plotting, but I can't see the direct benefit of it. The official documentation gives the following example, but its output doesn't make sense to me: x =…
HonzaB
  • 7,065
  • 6
  • 31
  • 42
463
votes
9 answers

How do I convert a PIL Image into a NumPy array?

How do I convert a PIL Image back and forth to a NumPy array so that I can do faster pixel-wise transformations than PIL's PixelAccess allows? I can convert it to a NumPy array via: pic = Image.open("foo.jpg") pix =…
akdom
  • 32,264
  • 27
  • 73
  • 79
430
votes
3 answers

What is the difference between flatten and ravel functions in numpy?

import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the same list. Then what is the need of two…
cryptomanic
  • 5,986
  • 3
  • 18
  • 30
413
votes
6 answers

Convert NumPy array to Python list

How do I convert a NumPy array into a Python List?
Alex Brooks
  • 5,133
  • 4
  • 21
  • 27
399
votes
7 answers

Concatenating two one-dimensional NumPy arrays

How do I concatenate two one-dimensional arrays in NumPy? I tried numpy.concatenate: import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5]) np.concatenate(a, b) But I get an error: TypeError: only length-1 arrays can be converted to…
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
374
votes
5 answers

What is the difference between ndarray and array in NumPy?

What is the difference between ndarray and array in NumPy? Where is their implementation in the NumPy source code?
flxb
  • 4,235
  • 3
  • 16
  • 11
370
votes
5 answers

How do I use np.newaxis?

What is numpy.newaxis and when should I use it? Using it on a 1-D array x produces: >>> x array([0, 1, 2, 3]) >>> x[np.newaxis, :] array([[0, 1, 2, 3]]) >>> x[:, np.newaxis] array([[0], [1], [2], [3]])
352
votes
22 answers

Convert array of indices to one-hot encoded array in NumPy

Given a 1D array of indices: a = array([1, 0, 3]) I want to one-hot encode this as a 2D array: b = array([[0,1,0,0], [1,0,0,0], [0,0,0,1]])
James Atwood
  • 4,289
  • 2
  • 17
  • 17
338
votes
18 answers

Better way to shuffle two numpy arrays in unison

I have two numpy arrays of different shapes, but with the same length (leading dimension). I want to shuffle each of them, such that corresponding elements continue to correspond -- i.e. shuffle them in unison with respect to their leading…
Josh Bleecher Snyder
  • 8,262
  • 3
  • 35
  • 37
291
votes
7 answers

How do I create a numpy array of all True or all False?

In Python, how do I create a numpy array of arbitrary shape filled with all True or all False?
Michael Currie
  • 13,721
  • 9
  • 42
  • 58
290
votes
12 answers

How do I calculate percentiles with python/numpy?

Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy array? I am looking for something similar to Excel's percentile function. I looked in NumPy's statistics reference, and couldn't find this. All I could…
Uri
  • 88,451
  • 51
  • 221
  • 321
105
votes
7 answers

Partition array into N chunks with Numpy

There is this How do you split a list into evenly sized chunks? for splitting an array into chunks. Is there anyway to do this more efficiently for giant arrays using Numpy?
Eiyrioü von Kauyf
  • 4,481
  • 7
  • 32
  • 41
1
2 3
99 100