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

2499 questions
602
votes
19 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], function(array, n=3) would…
Alexis Métaireau
  • 8,372
  • 5
  • 21
  • 34
523
votes
10 answers

What does -1 mean in numpy reshape?

A numpy matrix can be reshaped into a vector using reshape function with parameter -1. But I don't know what -1 means here. For example: a = numpy.matrix([[1, 2, 3, 4], [5, 6, 7, 8]]) b = numpy.reshape(a, -1) The result of b is: matrix([[1, 2, 3,…
user2262504
  • 5,628
  • 4
  • 15
  • 22
503
votes
5 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
  • 20,150
  • 25
  • 66
  • 105
385
votes
7 answers

What is the purpose of meshgrid in Python / NumPy?

Can someone explain to me what is the purpose of meshgrid function in Numpy? I know it creates some kind of grid of coordinates for plotting, but I can't really see the direct benefit of it. I am studying "Python Machine Learning" from Sebastian…
HonzaB
  • 5,435
  • 6
  • 23
  • 36
346
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,046
  • 2
  • 14
  • 27
324
votes
8 answers

How to convert a PIL Image into a numpy array?

Alright, I'm toying around with converting a PIL image object back and forth to a numpy array so I can do some faster pixel by pixel transformations than PIL's PixelAccess object would allow. I've figured out how to place the pixel information in a…
akdom
  • 28,041
  • 24
  • 70
  • 79
320
votes
6 answers

Concatenating two one-dimensional NumPy arrays

I have two simple one-dimensional arrays in NumPy. I should be able to concatenate them using numpy.concatenate. But I get this error for the code below: TypeError: only length-1 arrays can be converted to Python scalars Code import numpy a =…
highBandWidth
  • 14,815
  • 16
  • 74
  • 126
298
votes
5 answers

What is the difference between ndarray and array in numpy?

What is the difference between ndarray and array in Numpy? And where can I find the implementations in the numpy source code?
flxb
  • 3,395
  • 3
  • 14
  • 11
279
votes
16 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
  • 7,547
  • 2
  • 33
  • 36
269
votes
21 answers

Convert array of indices to 1-hot encoded numpy array

Let's say I have a 1d numpy array a = array([1,0,3]) I would like to encode this as a 2D one-hot array b = array([[0,1,0,0], [1,0,0,0], [0,0,0,1]]) Is there a quick way to do this? Quicker than just looping over a to set elements of b, that is.
James Atwood
  • 3,409
  • 2
  • 15
  • 17
257
votes
4 answers

How does numpy.newaxis work and when to use it?

When I try numpy.newaxis the result gives me a 2-d plot frame with x-axis from 0 to 1. However, when I try using numpy.newaxis to slice a vector, vector[0:4,] [ 0.04965172 0.04979645 0.04994022 0.05008303] vector[:, np.newaxis][0:4,] [[…
238
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
  • 84,589
  • 46
  • 214
  • 312
231
votes
7 answers

How to 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
  • 11,166
  • 7
  • 39
  • 54
72
votes
5 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
  • 3,730
  • 5
  • 28
  • 39
57
votes
5 answers

What is an intuitive explanation of np.unravel_index?

Pretty much what the title says. I've read the documentation and I've played with the function for a while now but I can't discern what the physical manifestation of this transformation is.
austinkjensen
  • 707
  • 1
  • 5
  • 10
1
2 3
99 100