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
303
votes
26 answers

How to implement the Softmax function in Python

From the Udacity's deep learning class, the softmax of y_i is simply the exponential divided by the sum of exponential of the whole Y vector: Where S(y_i) is the softmax function of y_i and e is the exponential and j is the no. of columns in the…
alvas
  • 115,346
  • 109
  • 446
  • 738
293
votes
12 answers

How to smooth a curve for a dataset

Lets assume we have a dataset which might be given approximately by import numpy as np x = np.linspace(0,2*np.pi,100) y = np.sin(x) + np.random.random(100) * 0.2 Therefore we have a variation of 20% of the dataset. My first idea was to use the…
varantir
  • 6,624
  • 6
  • 36
  • 57
293
votes
30 answers

Moving average or running mean

Is there a SciPy function or NumPy function or module for Python that calculates the running mean of a 1D array given a specific window?
Shejo284
  • 4,541
  • 6
  • 32
  • 44
291
votes
7 answers

How do I create a numpy array of all True or all False?

In Python, how do I create a numpy array of arbitrary shape filled with all True or all False?
Michael Currie
  • 13,721
  • 9
  • 42
  • 58
290
votes
12 answers

How do I calculate percentiles with python/numpy?

Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy array? I am looking for something similar to Excel's percentile function. I looked in NumPy's statistics reference, and couldn't find this. All I could…
Uri
  • 88,451
  • 51
  • 221
  • 321
284
votes
13 answers

How can I map True/False to 1/0 in a Pandas DataFrame?

I have a column in python pandas DataFrame that has boolean True/False values, but for further calculations I need 1/0 representation. Is there a quick pandas/numpy way to do that?
Simon Righley
  • 4,538
  • 6
  • 26
  • 33
282
votes
1 answer

numpy matrix vector multiplication

When I multiply two numpy arrays of sizes (n x n)*(n x 1), I get a matrix of size (n x n). Following normal matrix multiplication rules, an (n x 1) vector is expected, but I simply cannot find any information about how this is done in Python's Numpy…
user3272574
  • 2,987
  • 2
  • 12
  • 15
281
votes
9 answers

Take multiple lists into dataframe

How do I take multiple lists and put them as different columns in a python dataframe? I tried this solution but had some trouble. Attempt 1: Have three lists, and zip them together and use that res = zip(lst1,lst2,lst3) Yields just one…
jfalkson
  • 3,471
  • 4
  • 20
  • 25
281
votes
11 answers

Create numpy matrix filled with NaNs

I have the following code: r = numpy.zeros(shape = (width, height, 9)) It creates a width x height x 9 matrix filled with zeros. Instead, I'd like to know if there's a function or way to initialize them instead to NaNs in an easy way.
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
279
votes
8 answers

Relationship between SciPy and NumPy

SciPy appears to provide most (but not all [1]) of NumPy's functions in its own namespace. In other words, if there's a function named numpy.foo, there's almost certainly a scipy.foo. Most of the time, the two appear to be exactly the same,…
NPE
  • 486,780
  • 108
  • 951
  • 1,012
276
votes
4 answers

How can I check whether a numpy array is empty or not?

How can I check whether a numpy array is empty or not? I used the following code, but this fails if the array contains a zero. if not self.Definition.all(): Is this the solution? if self.Definition == array([]):
Kicsi Mano
  • 3,551
  • 3
  • 21
  • 26
274
votes
15 answers

Transposing a 1D NumPy array

I use Python and NumPy and have some problems with "transpose": import numpy as np a = np.array([5,4]) print(a) print(a.T) Invoking a.T is not transposing the array. If a is for example [[],[]] then it transposes correctly, but I need the…
thaking
  • 3,495
  • 8
  • 27
  • 33
273
votes
8 answers

Replace all elements of NumPy array that are greater than some value

I have a 2D NumPy array. How do I replace all values in it greater than a threshold T = 255 with a value x = 255? A slow for-loop based method would be: # arr = arr.copy() # Optionally, do not modify original arr. for i in range(arr.shape[0]): …
NLi10Me
  • 3,182
  • 2
  • 13
  • 15
270
votes
3 answers

How to convert a NumPy array to PIL image applying matplotlib colormap

I have a simple problem, but I cannot find a good solution to it. I want to take a NumPy 2D array which represents a grayscale image, and convert it to an RGB PIL image while applying some of the matplotlib colormaps. I can get a reasonable PNG…
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
267
votes
10 answers

Is it possible to use argsort in descending order?

Consider the following code: avgDists = np.array([1, 8, 6, 9, 4]) ids = avgDists.argsort()[:n] This gives me indices of the n smallest elements. Is it possible to use this same argsort in descending order to get the indices of n highest elements?
shn
  • 5,116
  • 9
  • 34
  • 62