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
430
votes
3 answers

What is the difference between flatten and ravel functions in numpy?

import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the same list. Then what is the need of two…
cryptomanic
  • 5,986
  • 3
  • 18
  • 30
426
votes
3 answers

Simple Digit Recognition OCR in OpenCV-Python

I am trying to implement a "Digit Recognition OCR" in OpenCV-Python (cv2). It is just for learning purposes. I would like to learn both KNearest and SVM features in OpenCV. I have 100 samples (i.e. images) of each digit. I would like to train with…
Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
424
votes
8 answers

Difference between numpy.array shape (R, 1) and (R,)

In numpy, some of the operations return in shape (R, 1) but some return (R,). This will make matrix multiplication more tedious since explicit reshape is required. For example, given a matrix M, if we want to do numpy.dot(M[:,0], numpy.ones((1, R)))…
clwen
  • 20,004
  • 31
  • 77
  • 94
423
votes
10 answers

Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

I have a Numpy array consisting of a list of lists, representing a two-dimensional array with row labels and column names as shown below: data = np.array([['','Col1','Col2'],['Row1',1,2],['Row2',3,4]]) I'd like the resulting DataFrame to have Row1…
user3132783
  • 5,275
  • 3
  • 15
  • 7
419
votes
24 answers

Saving a Numpy array as an image

I have a matrix in the type of a Numpy array. How would I write it to disk it as an image? Any format works (png, jpeg, bmp...). One important constraint is that PIL is not present.
M456
  • 5,547
  • 2
  • 19
  • 14
413
votes
6 answers

Convert NumPy array to Python list

How do I convert a NumPy array into a Python List?
Alex Brooks
  • 5,133
  • 4
  • 21
  • 27
406
votes
27 answers

What does axis in pandas mean?

Here is my code to generate a dataframe: import pandas as pd import numpy as np dff = pd.DataFrame(np.random.randn(1,2),columns=list('AB')) then I got the dataframe: +------------+---------+--------+ | | A | B …
jerry_sjtu
  • 5,216
  • 8
  • 29
  • 42
399
votes
7 answers

Concatenating two one-dimensional NumPy arrays

How do I concatenate two one-dimensional arrays in NumPy? I tried numpy.concatenate: import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5]) np.concatenate(a, b) But I get an error: TypeError: only length-1 arrays can be converted to…
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
388
votes
17 answers

Frequency counts for unique values in a NumPy array

How do I efficiently obtain the frequency count for each unique value in a NumPy array? >>> x = np.array([1,1,1,2,2,2,5,25,1,1]) >>> freq_count(x) [(1, 5), (2, 3), (5, 1), (25, 1)]
Abe
  • 22,738
  • 26
  • 82
  • 111
384
votes
10 answers

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Let x be a NumPy array. The following: (x > 1) and (x < 3) Gives the error message: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() How do I fix this?
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
379
votes
10 answers

Dropping infinite values from dataframes in pandas?

How do I drop nan, inf, and -inf values from a DataFrame without resetting mode.use_inf_as_null? Can I tell dropna to include inf in its definition of missing values so that the following works? df.dropna(subset=["col1", "col2"], how="all")
user248237
375
votes
9 answers

NumPy array initialization (fill with identical values)

I need to create a NumPy array of length n, each element of which is v. Is there anything better than: a = empty(n) for i in range(n): a[i] = v I know zeros and ones would work for v = 0, 1. I could use v * ones(n), but it won't work when v is…
max
  • 49,282
  • 56
  • 208
  • 355
374
votes
5 answers

What is the difference between ndarray and array in NumPy?

What is the difference between ndarray and array in NumPy? Where is their implementation in the NumPy source code?
flxb
  • 4,235
  • 3
  • 16
  • 11
370
votes
5 answers

How do I use np.newaxis?

What is numpy.newaxis and when should I use it? Using it on a 1-D array x produces: >>> x array([0, 1, 2, 3]) >>> x[np.newaxis, :] array([[0, 1, 2, 3]]) >>> x[:, np.newaxis] array([[0], [1], [2], [3]])
369
votes
17 answers

How do I check which version of NumPy I'm using?

How can I check which version of NumPy I'm using?
larus
  • 4,359
  • 4
  • 21
  • 13