Questions tagged [numpy]
NumPy is an extension of the Python language that adds support to large multidimensional arrays and matrixes, along with a large library of high-level mathematical functions for operations with these arrays.
100,255
questions
775votes
21answers
850kviews
How to print the full NumPy array, without truncation?
When I print a numpy array, I get a truncated representation, but I want the full array.
Is there any way to do this?
Examples:
>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, ...
700votes
23answers
1.2mviews
How can the Euclidean distance be calculated with NumPy?
I have two points in 3D:
(xa, ya, za)
(xb, yb, zb)
And I want to calculate the distance:
dist = sqrt((xa-xb)^2 + (ya-yb)^2 + (za-zb)^2)
What's the best way to do this with NumPy, or with Python in ...
696votes
11answers
1.0mviews
Dump a NumPy array into a csv file
Is there a way to dump a NumPy array into a CSV file? I have a 2D NumPy array and need to dump it in human-readable format.
689votes
20answers
578kviews
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 ...
630votes
15answers
1.3mviews
Convert pandas dataframe to NumPy array
I am interested in knowing how to 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....
588votes
8answers
1.0mviews
How to access the ith column of a NumPy multidimensional array?
Suppose I have:
test = numpy.array([[1, 2], [3, 4], [5, 6]])
test[i] gets me ith line of the array (eg [1, 2]). How can I access the ith column? (eg [1, 3, 5]). Also, would this be an expensive ...
588votes
11answers
494kviews
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....
575votes
17answers
866kviews
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:
>>> l = [1, 2, 3]
>>> l.index(2)
1
Is there something like that for NumPy arrays?
546votes
31answers
854kviews
How to count the occurrence of certain item in an ndarray?
In Python, I have an ndarray y
that is printed as array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
I'm trying to count how many 0s and how many 1s are there in this array.
But when I type y.count(0) or ...
545votes
11answers
785kviews
Most efficient way to map function over numpy array
What is the most efficient way to map a function over a numpy array? The way I've been doing it in my current project is as follows:
import numpy as np
x = np.array([1, 2, 3, 4, 5])
# Obtain array ...
529votes
7answers
217kviews
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 ...
525votes
17answers
639kviews
How to find all occurrences of an element in a list
index() will give the first occurrence of an item in a list. Is there a neat trick which returns all indices in a list for an element?
518votes
7answers
947kviews
pandas create new column based on values from other columns / apply a function of multiple columns, row-wise
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 ...
508votes
13answers
1.0mviews
How do I read CSV data into a record array in NumPy?
I wonder if there is a direct way to import the contents of a CSV file into a record array, much in the way that R's read.table(), read.delim(), and read.csv() family imports data to R's data frame?
...
461votes
11answers
501kviews
Pandas read_csv low_memory and dtype options
When calling
df = pd.read_csv('somefile.csv')
I get:
/Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/io/parsers.py:1130:
DtypeWarning: Columns (4,5,7,16) have mixed types. ...