Questions tagged [numpy]

NumPy is one of the many modules in Python that adds support of large multidimensional arrays and matrixes, along with a large library of high-level mathematical functions for operations with these arrays. --- To install the numpy module enter this command line: ~~~ python -m pip install numpy ~~~ It may require the wheel and pip package.

Filter by
Sorted by
Tagged with
868 votes
23 answers
1.1m 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(...
kame's user avatar
  • 20.9k
802 votes
21 answers
726k 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, ...
Alexis Métaireau's user avatar
785 votes
13 answers
1.2m 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?
Dexter's user avatar
  • 11.4k
779 votes
26 answers
1.4m 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: ...
Nathan Fellman's user avatar
693 votes
16 answers
1.6m views

Convert pandas dataframe to NumPy array

How do I convert a pandas dataframe into a NumPy array? DataFrame: import numpy as np import pandas as pd index = [1, 2, 3, 4, 5, 6, 7] a = [np.nan, np.nan, np.nan, 0.1, 0.1, 0.1, 0.1] b = [0.2, np....
Mister Nobody's user avatar
664 votes
12 answers
651k 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]]) ...
user2262504's user avatar
  • 7,087
658 votes
10 answers
1.2m 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?
lpl's user avatar
  • 6,629
649 votes
11 answers
1.0m 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 = ...
Ryan's user avatar
  • 7,681
646 votes
21 answers
1.0m 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?
Nope's user avatar
  • 34.8k
621 votes
32 answers
1.0m 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
mflowww's user avatar
  • 6,885
606 votes
8 answers
1.2m views

Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas

I want to apply my custom function (it uses an if-else ladder) to these six columns (ERI_Hispanic, ERI_AmerInd_AKNatv, ERI_Asian, ERI_Black_Afr.Amer, ERI_HI_PacIsl, ERI_White) in each row of my ...
Dave's user avatar
  • 6,968
560 votes
14 answers
1.2m 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() ...
hatmatrix's user avatar
  • 43k
558 votes
7 answers
242k 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 ...
Thomas Browne's user avatar
553 votes
13 answers
685k 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=...
Josh's user avatar
  • 12k
551 votes
8 answers
839k 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?
Amani's user avatar
  • 16.4k
506 votes
16 answers
510k 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: ...
user avatar
492 votes
13 answers
810k views

How do I create a new column where the values are selected based on existing columns?

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
user7289's user avatar
  • 32.7k
489 votes
12 answers
662k views

What does numpy.random.seed(0) do?

What does np.random.seed do? np.random.seed(0)
covariance's user avatar
  • 6,853
487 votes
9 answers
340k 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, ...
HonzaB's user avatar
  • 7,075
480 votes
20 answers
548k views

Find nearest value in numpy array

How do I find the nearest value in a numpy array? Example: np.find_nearest(array, value)
Fookatchu's user avatar
  • 7,135
466 votes
9 answers
864k 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(&...
akdom's user avatar
  • 32.4k
455 votes
14 answers
597k 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, ...
camillio's user avatar
  • 4,565
454 votes
14 answers
791k 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 ...
Andy Hayden's user avatar
446 votes
8 answers
183k 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.
Benjamin Hodgson's user avatar
444 votes
7 answers
201k views

What are the differences between numpy arrays and matrices? Which one should I use?

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 ...
levesque's user avatar
  • 8,786
442 votes
17 answers
804k 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], ])
Peter Smit's user avatar
  • 27.8k
442 votes
16 answers
1.5m 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?
Ben's user avatar
  • 67k
435 votes
8 answers
692k 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: >>> ...
clstaudt's user avatar
  • 21.5k
434 votes
16 answers
495k 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) ...
Karnivaurus's user avatar
  • 22.9k
432 votes
10 answers
1.0m 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]])
Cristi P's user avatar
  • 6,041
431 votes
3 answers
124k 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 ...
cryptomanic's user avatar
  • 6,006
426 votes
8 answers
227k 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 ...
clwen's user avatar
  • 20.1k
426 votes
3 answers
266k 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. ...
Abid Rahman K's user avatar
423 votes
10 answers
1.2m 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]])...
user3132783's user avatar
  • 5,285
420 votes
24 answers
909k 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.
M456's user avatar
  • 5,557
413 votes
6 answers
615k views

Convert NumPy array to Python list

How do I convert a NumPy array into a Python List?
Alex Brooks's user avatar
  • 5,133
408 votes
27 answers
460k views

What does axis in pandas mean?

Here is my code to generate a dataframe: import pandas as pd import numpy as np dff = pd.DataFrame(np.random.randn(1,2),columns=list('AB')) then I got the dataframe: +------------+---------+-------...
jerry_sjtu's user avatar
  • 5,236
400 votes
7 answers
531k 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: ...
highBandWidth's user avatar
392 votes
17 answers
519k 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)]
Abe's user avatar
  • 22.8k
384 votes
10 answers
1.6m 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 ...
Homunculus Reticulli's user avatar
380 votes
10 answers
517k 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....
user avatar
377 votes
9 answers
647k 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 ...
max's user avatar
  • 49.5k
375 votes
5 answers
188k 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?
flxb's user avatar
  • 4,245
370 votes
17 answers
610k views

How do I check which version of NumPy I'm using?

How can I check which version of NumPy I'm using?
larus's user avatar
  • 4,369
370 votes
5 answers
432k 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....
Yue Harriet Huang's user avatar
368 votes
4 answers
463k views

Convert 2D float array to 2D int array in NumPy

How do I convert a float NumPy array into an int NumPy array?
Shan's user avatar
  • 18.6k
363 votes
27 answers
401k 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 ...
Vincent's user avatar
  • 16k
360 votes
13 answers
426k 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 -> "...
conradlee's user avatar
  • 13.1k
360 votes
8 answers
571k 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: ...
nye17's user avatar
  • 12.9k
359 votes
11 answers
853k 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 ...
jlswint's user avatar
  • 3,693

1
2 3 4 5
2253