115,291 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(...
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?
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, ...
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]])
...
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
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=...
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() ...
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:
...
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
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)
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, ...
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) ...
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:
>>> ...
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 ...
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]])
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
7
answers
719k
views
Convert NumPy array to Python list
How do I convert a NumPy array into a Python List?
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. ...
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 ...
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)]
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
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 -> "...
391
votes
17
answers
763k
views
How do I check which version of NumPy I'm using?
How can I check which version of NumPy I'm using?
379
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)
374
votes
27
answers
446k
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 ...