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.

About NumPy

From the NumPy homepage:

NumPy is the fundamental package for scientific computing with . It contains among other things:

  • a powerful N-dimensional array object
  • built-in universal functions (ufuncs)
  • sophisticated (broadcasting) operation
  • tools for integrating C/C++ and Fortran code
  • useful linear algebra, Fourier transform, and random number capabilities

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

NumPy provides reliable and efficient methods of data storage, manipulation, and analysis as it also integrates easily with other methods of data manipulation, notably Pandas and scikit-learn.

NumPy is released under the BSD license, enabling reuse with few restrictions.

Resources

Official Resources

Books

112481 questions
863
votes
22 answers

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(10000).reshape(250,40) array([[ 0, 1, 2, ..., 37, 38, …
kame
  • 20,848
  • 33
  • 104
  • 159
797
votes
21 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], then nargmax(array, n=3) would…
Alexis Métaireau
  • 10,767
  • 5
  • 24
  • 34
779
votes
13 answers

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
  • 11,311
  • 11
  • 45
  • 61
776
votes
26 answers

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: import numpy a = numpy.array((ax, ay, az)) b =…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
691
votes
16 answers

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.nan, 0.2, 0.2, 0.2, np.nan, np.nan] c = [np.nan,…
Mister Nobody
  • 6,927
  • 3
  • 13
  • 3
661
votes
12 answers

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]]) Usually, array[-1] means the last element. But what does -1 mean here?
user2262504
  • 7,057
  • 5
  • 18
  • 23
656
votes
10 answers

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
  • 6,609
  • 3
  • 15
  • 6
643
votes
20 answers

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
  • 34,682
  • 42
  • 94
  • 119
643
votes
11 answers

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 = lambda t: t ** 2 squares = np.array([squarer(xi)…
Ryan
  • 7,621
  • 5
  • 18
  • 31
617
votes
32 answers

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
  • 6,835
  • 6
  • 18
  • 18
606
votes
8 answers

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 dataframe. I've tried different methods from other…
Dave
  • 6,968
  • 7
  • 26
  • 32
560
votes
14 answers

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() and then apply numpy.core.records.fromrecords()?
hatmatrix
  • 42,883
  • 45
  • 137
  • 231
555
votes
7 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
  • 23,824
  • 32
  • 78
  • 121
550
votes
13 answers

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=False. Why is the dtype option related to…
Josh
  • 11,979
  • 17
  • 60
  • 96
548
votes
8 answers

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
  • 16,245
  • 29
  • 103
  • 153
1
2 3
99 100