115,289 questions
925
votes
23
answers
1.3m
views
How do I print the full NumPy array, without truncation?
When I print a numpy array, I get a truncated representation, but I want the full array.
>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(...
837
votes
21
answers
812k
views
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, ...
827
votes
13
answers
1.4m
views
Dump a NumPy array into a csv file
How do I dump a 2D NumPy array into a csv file in a human-readable format?
810
votes
26
answers
1.5m
views
How can the Euclidean distance be calculated with NumPy?
I have two points in 3D space:
a = (ax, ay, az)
b = (bx, by, bz)
I want to calculate the distance between them:
dist = sqrt((ax-bx)^2 + (ay-by)^2 + (az-bz)^2)
How do I do this with NumPy? I have:
...
718
votes
17
answers
1.7m
views
Convert Pandas dataframe to NumPy array
How do I convert a Pandas dataframe into a NumPy array?
import numpy as np
import pandas as pd
df = pd.DataFrame(
{
'A': [np.nan, np.nan, np.nan, 0.1, 0.1, 0.1, 0.1],
'B': [0.2, ...
714
votes
12
answers
1.2m
views
Most efficient way to map function over numpy array
What is the most efficient way to map a function over a numpy array? I am currently doing:
import numpy as np
x = np.array([1, 2, 3, 4, 5])
# Obtain array of square of each element in x
squarer = ...
695
votes
12
answers
727k
views
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]])
...
686
votes
10
answers
1.3m
views
How do I access the ith column of a NumPy multidimensional array?
Given:
test = np.array([[1, 2], [3, 4], [5, 6]])
test[i] gives the ith row (e.g. [1, 2]). How do I access the ith column? (e.g. [1, 3, 5]). Also, would this be an expensive operation?
678
votes
32
answers
1.2m
views
How do I count the occurrence of a certain item in an ndarray?
How do I count the number of 0s and 1s in the following array?
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
y.count(0) gives:
numpy.ndarray object has no attribute count
677
votes
24
answers
1.2m
views
Is there a NumPy function to return the first index of something in an array?
I know there is a method for a Python list to return the first index of something:
>>> xs = [1, 2, 3]
>>> xs.index(2)
1
Is there something like that for NumPy arrays?
598
votes
14
answers
837k
views
Pandas read_csv: low_memory and dtype options
df = pd.read_csv('somefile.csv')
...gives an error:
.../site-packages/pandas/io/parsers.py:1130:
DtypeWarning: Columns (4,5,7,16) have mixed types. Specify dtype
option on import or set low_memory=...
587
votes
8
answers
1.0m
views
How can I use the apply() function for a single column?
I have a pandas dataframe with multiple columns. I want to change the values of the only the first column without affecting the other columns. How can I do that using apply() in pandas?
580
votes
14
answers
1.3m
views
How do I read CSV data into a record array in NumPy?
Is there a direct way to import the contents of a CSV file into a record array, just like how R's read.table(), read.delim(), and read.csv() import data into R dataframes?
Or should I use csv.reader() ...
574
votes
8
answers
264k
views
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 ...
520
votes
16
answers
566k
views
Sorting arrays in NumPy by column
How do I sort a NumPy array by its nth column?
For example, given:
a = array([[9, 2, 3],
[4, 5, 6],
[7, 0, 5]])
I want to sort the rows of a by the second column to obtain:
...
517
votes
12
answers
785k
views
What does numpy.random.seed(0) do?
What does np.random.seed do?
np.random.seed(0)
510
votes
15
answers
870k
views
How do I create a new column where the values are selected based on an existing column?
How do I add a color column to the following dataframe so that color='green' if Set == 'Z', and color='red' otherwise?
Type Set
1 A Z
2 B Z
3 B X
4 C Y
505
votes
10
answers
380k
views
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, ...
499
votes
21
answers
630k
views
Find nearest value in numpy array
How do I find the nearest value in a numpy array? Example:
np.find_nearest(array, value)
497
votes
8
answers
988k
views
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(&...
494
votes
14
answers
674k
views
Pretty-print a NumPy array without scientific notation and with given precision
How do I print formatted NumPy arrays in a way similar to this:
x = 1.23456
print('%.3f' % x)
If I want to print the numpy.ndarray of floats, it prints several decimals, often in 'scientific' format, ...
483
votes
14
answers
913k
views
Converting between datetime, Timestamp and datetime64
How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)?
In the following code, I create a datetime, timestamp and datetime64 objects.
import datetime
import numpy as np
...
480
votes
17
answers
595k
views
NumPy array is not JSON serializable
After creating a NumPy array, and saving it as a Django context variable, I receive the following error when loading the webpage:
array([ 0, 239, 479, 717, 952, 1192, 1432, 1667], dtype=int64) ...
474
votes
8
answers
203k
views
What is the difference between np.array() and np.asarray()?
What is the difference between NumPy's np.array and np.asarray? When should I use one rather than the other? They seem to generate identical output.
465
votes
9
answers
807k
views
Comparing two NumPy arrays for equality, element-wise
What is the simplest way to compare two NumPy arrays for equality (where equality is defined as: A = B iff for all indices i: A[i] == B[i])?
Simply using == gives me a boolean array:
>>> ...
463
votes
17
answers
871k
views
How do I add an extra column to a NumPy array?
Given the following 2D array:
a = np.array([
[1, 2, 3],
[2, 3, 4],
])
I want to add a column of zeros along the second axis to get:
b = np.array([
[1, 2, 3, 0],
[2, 3, 4, 0],
])
460
votes
4
answers
146k
views
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 ...
458
votes
17
answers
1.6m
views
How do I create an empty array and then append to it in NumPy?
I want to create an empty array and append items to it, one at a time.
xs = []
for item in data:
xs.append(item)
Can I use this list-style notation with NumPy arrays?
450
votes
10
answers
1.1m
views
Numpy array dimensions
How do I get the dimensions of an array? For instance, this is 2x2:
a = np.array([[1, 2], [3, 4]])
450
votes
7
answers
220k
views
What are the differences between numpy arrays and matrices? Which one should I use? [closed]
What are the advantages and disadvantages of each?
From what I've seen, either one can work as a replacement for the other if need be, so should I bother using both or should I stick to just one of ...
447
votes
24
answers
1.0m
views
Saving a Numpy array as an image
I have a matrix in the type of a Numpy array. How would I write it to disk it as an image? Any format works (png, jpeg, bmp...). One important constraint is that PIL is not present.
444
votes
10
answers
1.4m
views
Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?
I have a Numpy array consisting of a list of lists, representing a two-dimensional array with row labels and column names as shown below:
data = np.array([['','Col1','Col2'],['Row1',1,2],['Row2',3,4]])...
444
votes
7
answers
719k
views
Convert NumPy array to Python list
How do I convert a NumPy array into a Python List?
438
votes
8
answers
249k
views
Difference between numpy.array shape (R, 1) and (R,)
In numpy, some of the operations return in shape (R, 1) but some return (R,). This will make matrix multiplication more tedious since explicit reshape is required. For example, given a matrix M, if we ...
432
votes
3
answers
278k
views
Simple Digit Recognition OCR in OpenCV-Python
I am trying to implement a "Digit Recognition OCR" in OpenCV-Python (cv2). It is just for learning purposes. I would like to learn both KNearest and SVM features in OpenCV.
I have 100 samples (i.e. ...
422
votes
7
answers
593k
views
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:
...
421
votes
11
answers
1.9m
views
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Let x be a NumPy array. The following:
(x > 1) and (x < 3)
Gives the error message:
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
How ...
411
votes
9
answers
755k
views
NumPy array initialization (fill with identical values) [duplicate]
I need to create a NumPy array of length n, each element of which is v.
Is there anything better than:
a = empty(n)
for i in range(n):
a[i] = v
I know zeros and ones would work for v = 0, 1. I ...
405
votes
17
answers
588k
views
Frequency counts for unique values in a NumPy array
How do I efficiently obtain the frequency count for each unique value in a NumPy array?
>>> x = np.array([1,1,1,2,2,2,5,25,1,1])
>>> freq_count(x)
[(1, 5), (2, 3), (5, 1), (25, 1)]
404
votes
6
answers
228k
views
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?
402
votes
10
answers
588k
views
Dropping infinite values from dataframes in pandas?
How do I drop nan, inf, and -inf values from a DataFrame without resetting mode.use_inf_as_null?
Can I tell dropna to include inf in its definition of missing values so that the following works?
df....
396
votes
5
answers
527k
views
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....
396
votes
13
answers
510k
views
Converting numpy dtypes to native python types
If I have a numpy dtype, how do I automatically convert it to its closest python data type? For example,
numpy.float32 -> "python float"
numpy.float64 -> "python float"
numpy.uint32 -> "...
393
votes
13
answers
883k
views
How do I remove NaN values from a NumPy array?
How do I remove NaN values from a NumPy array?
[1, 2, NaN, 4, NaN, 8] ⟶ [1, 2, 4, 8]
391
votes
17
answers
764k
views
How do I check which version of NumPy I'm using?
How can I check which version of NumPy I'm using?
380
votes
8
answers
202k
views
Understanding NumPy's einsum
How does np.einsum work?
Given arrays A and B, their matrix multiplication followed by transpose is computed using (A @ B).T, or equivalently, using:
np.einsum("ij, jk -> ki", A, B)
380
votes
8
answers
640k
views
Most efficient way to reverse a numpy array
Believe it or not, after profiling my current code, the repetitive operation of numpy array reversion ate a giant chunk of the running time. What I have right now is the common view-based method:
...
378
votes
11
answers
960k
views
How do I convert a numpy array to (and display) an image?
I have created an array thusly:
import numpy as np
data = np.zeros( (512,512,3), dtype=np.uint8)
data[256,256] = [255,0,0]
What I want this to do is display a single red dot in the center of a ...
374
votes
27
answers
447k
views
Split (explode) pandas dataframe string entry to separate rows
I have a pandas dataframe in which one column of text strings contains comma-separated values. I want to split each CSV field and create a new row per entry (assume that CSV are clean and need only be ...
369
votes
4
answers
470k
views
Convert 2D float array to 2D int array in NumPy
How do I convert a float NumPy array into an int NumPy array?