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
232
votes
7 answers

How to add a new row to an empty numpy array

Using standard Python arrays, I can do the following: arr = [] arr.append([1,2,3]) arr.append([4,5,6]) # arr is now [[1,2,3],[4,5,6]] However, I cannot do the same thing in numpy. For example: arr = np.array([]) arr = np.append(arr,…
Tony Stark
  • 3,353
  • 7
  • 26
  • 30
232
votes
12 answers

"Cloning" row or column vectors

Sometimes it is useful to "clone" a row or column vector to a matrix. By cloning I mean converting a row vector such as [1, 2, 3] Into a matrix [[1, 2, 3], [1, 2, 3], [1, 2, 3]] or a column vector such as [[1], [2], [3]] into [[1, 1, 1] [2,…
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
230
votes
7 answers

How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting

I have a set of data and I want to compare which line describes it best (polynomials of different orders, exponential or logarithmic). I use Python and Numpy and for polynomial fitting there is a function polyfit(). But I found no such functions for…
Tomas Novotny
  • 7,547
  • 9
  • 26
  • 23
230
votes
35 answers

ImportError: numpy.core.multiarray failed to import

I'm trying to run this program import cv2 import time cv.NamedWindow("camera", 1) capture = cv.CaptureFromCAM(0) while True: img = cv.QueryFrame(capture) cv.ShowImage("camera", img) if cv.WaitKey(10) == 27: …
user3090952
230
votes
10 answers

Numpy: Get random set of rows from 2D array

I have a very large 2D array which looks something like this: a= [[a1, b1, c1], [a2, b2, c2], ..., [an, bn, cn]] Using numpy, is there an easy way to get a new 2D array with, e.g., 2 random rows from the initial array a (without…
gha
  • 3,459
  • 3
  • 18
  • 13
228
votes
11 answers

Extracting specific columns in numpy array

This is an easy question but say I have an MxN matrix. All I want to do is extract specific columns and store them in another numpy array but I get invalid syntax errors. Here is the code: extractedData = data[[:,1],[:,9]]. It seems like the above…
Aladdin
  • 2,369
  • 2
  • 17
  • 11
228
votes
9 answers

A column-vector y was passed when a 1d array was expected

I need to fit RandomForestRegressor from sklearn.ensemble. forest = ensemble.RandomForestRegressor(**RF_tuned_parameters) model = forest.fit(train_fold, train_y) yhat = model.predict(test_fold) This code always worked until I made some…
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
226
votes
19 answers

How to calculate rolling / moving average using python + NumPy / SciPy?

There seems to be no function that simply calculates the moving average on numpy/scipy, leading to convoluted solutions. My question is two-fold: What's the easiest way to (correctly) implement a moving average with numpy? Since this seems…
loopbackbee
  • 21,962
  • 10
  • 62
  • 97
225
votes
4 answers

Python memory usage of numpy arrays

I'm using python to analyse some large files and I'm running into memory issues, so I've been using sys.getsizeof() to try and keep track of the usage, but it's behaviour with numpy arrays is bizarre. Here's an example involving a map of albedos…
EddyTheB
  • 3,100
  • 4
  • 23
  • 32
224
votes
10 answers

NumPy or Pandas: Keeping array type as integer while having a NaN value

Is there a preferred way to keep the data type of a numpy array fixed as int (or int64 or whatever), while still having an element inside listed as numpy.NaN? In particular, I am converting an in-house data structure to a Pandas DataFrame. In our…
ely
  • 74,674
  • 34
  • 147
  • 228
223
votes
18 answers

Calculating Pearson correlation and significance in Python

I am looking for a function that takes as input two lists, and returns the Pearson correlation, and the significance of the correlation.
ariel
  • 2,239
  • 2
  • 14
  • 3
222
votes
19 answers

ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject

Importing from pyxdameraulevenshtein gives the following error, I have pyxdameraulevenshtein==1.5.3 pandas==1.1.4 scikit-learn==0.20.2. Numpy is 1.16.1. Works well in Python 3.6, Issue in Python 3.7. Has anyone been facing similar issues with…
Sachit Jani
  • 2,321
  • 2
  • 4
  • 5
219
votes
8 answers

Numpy first occurrence of value greater than existing value

I have a 1D array in numpy and I want to find the position of the index where a value exceeds the value in numpy array. E.g. aa = range(-10,10) Find position in aa where, the value 5 gets exceeded.
user308827
  • 21,227
  • 87
  • 254
  • 417
218
votes
8 answers

How to normalize a NumPy array to within a certain range?

After doing some processing on an audio or image array, it needs to be normalized within a range before it can be written back to a file. This can be done like so: # Normalize audio channels to between -1.0 and +1.0 audio[:,0] =…
endolith
  • 25,479
  • 34
  • 128
  • 192
215
votes
9 answers

Numpy where function multiple conditions

I have an array of distances called dists. I want to select dists which are within a range. dists[(np.where(dists >= r)) and (np.where(dists <= r + dr))] However, this selects only for the condition (np.where(dists <= r + dr)) If I do the…
user1654183
  • 4,375
  • 6
  • 26
  • 33