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
368
votes
4 answers

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

How do I convert a float NumPy array into an int NumPy array?
Shan
  • 18,563
  • 39
  • 97
  • 132
362
votes
27 answers

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 split on ','). For example, a should become…
Vincent
  • 15,809
  • 7
  • 37
  • 39
360
votes
13 answers

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 -> "python int" numpy.int16 -> "python int" I could try to…
conradlee
  • 12,985
  • 17
  • 57
  • 93
358
votes
8 answers

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: reversed_arr = arr[::-1] Is there any other way to…
nye17
  • 12,857
  • 11
  • 58
  • 68
358
votes
11 answers

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 512x512 image. (At least to begin with... I think I can…
jlswint
  • 3,683
  • 2
  • 16
  • 4
354
votes
13 answers

How do I remove NaN values from a NumPy array?

How do I remove NaN values from a NumPy array? [1, 2, NaN, 4, NaN, 8] ⟶ [1, 2, 4, 8]
Dax Feliz
  • 12,220
  • 8
  • 30
  • 33
352
votes
22 answers

Convert array of indices to one-hot encoded array in NumPy

Given a 1D array of indices: a = array([1, 0, 3]) I want to one-hot encode this as a 2D array: b = array([[0,1,0,0], [1,0,0,0], [0,0,0,1]])
James Atwood
  • 4,289
  • 2
  • 17
  • 17
349
votes
8 answers

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)
Lance Strait
  • 4,001
  • 4
  • 17
  • 18
338
votes
18 answers

Better way to shuffle two numpy arrays in unison

I have two numpy arrays of different shapes, but with the same length (leading dimension). I want to shuffle each of them, such that corresponding elements continue to correspond -- i.e. shuffle them in unison with respect to their leading…
Josh Bleecher Snyder
  • 8,262
  • 3
  • 35
  • 37
336
votes
13 answers

How to remove specific elements in a numpy array

How can I remove some specific elements from a numpy array? Say I have import numpy as np a = np.array([1,2,3,4,5,6,7,8,9]) I then want to remove 3,4,7 from a. All I know is the index of the values (index=[2,3,6]).
325
votes
14 answers

Convert a tensor to numpy array in Tensorflow?

How to convert a tensor into a numpy array when using Tensorflow with Python bindings?
mathetes
  • 11,766
  • 7
  • 25
  • 32
319
votes
27 answers

Error "Import Error: No module named numpy" on Windows

I have a very similar question to this question, but I am still one step behind. I have only one version of Python 3 installed on my Windows 7 (sorry) 64-bit system. I installed NumPy following this link - as suggested in the question. The…
Seb
  • 3,655
  • 3
  • 17
  • 17
319
votes
11 answers

How to take column-slices of dataframe in pandas

I load some machine learning data from a CSV file. The first 2 columns are observations and the remaining columns are features. Currently, I do the following: data = pandas.read_csv('mydata.csv') which gives something like: data =…
cpa
  • 3,742
  • 4
  • 16
  • 22
310
votes
15 answers

How to normalize a numpy array to a unit vector

I would like to convert a NumPy array to a unit vector. More specifically, I am looking for an equivalent version of this normalisation function: def normalize(v): norm = np.linalg.norm(v) if norm == 0: return v return v /…
Donbeo
  • 17,067
  • 37
  • 114
  • 188
304
votes
9 answers

ValueError: setting an array element with a sequence

Why do the following code samples: np.array([[1, 2], [2, 3, 4]]) np.array([1.2, "abc"], dtype=float) ...all give the following error? ValueError: setting an array element with a sequence.
MedicalMath
  • 3,067
  • 2
  • 15
  • 5