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
259
votes
18 answers

Replacing Pandas or Numpy Nan with a None to use with MysqlDB

I am trying to write a Pandas dataframe (or can use a numpy array) to a mysql database using MysqlDB . MysqlDB doesn't seem understand 'nan' and my database throws out an error saying nan is not in the field list. I need to find a way to convert the…
FinDev
  • 4,437
  • 6
  • 29
  • 29
259
votes
9 answers

List of lists into numpy array

How do I convert a simple list of lists into a numpy array? The rows are individual sublists and each row contains the elements in the sublist.
Richa Sachdev
  • 2,937
  • 3
  • 17
  • 12
257
votes
5 answers

np.mean() vs np.average() in Python NumPy?

I notice that In [30]: np.mean([1, 2, 3]) Out[30]: 2.0 In [31]: np.average([1, 2, 3]) Out[31]: 2.0 However, there should be some differences, since after all they are two different functions. What are the differences between them?
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
257
votes
20 answers

Find unique rows in numpy.array

I need to find unique rows in a numpy.array. For example: >>> a # I have array([[1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 0]]) >>> new_a # I want to get to array([[1,…
Akavall
  • 82,592
  • 51
  • 207
  • 251
256
votes
8 answers

Counting unique values in a column in pandas dataframe like in Qlik?

If I have a table like this: df = pd.DataFrame({ 'hID': [101, 102, 103, 101, 102, 104, 105, 101], 'dID': [10, 11, 12, 10, 11, 10, 12, 10], 'uID': ['James', 'Henry', 'Abe', 'James', 'Henry', 'Brian', 'Claude', 'James'], …
Alhpa Delta
  • 3,385
  • 4
  • 16
  • 31
248
votes
13 answers

How to split data into 3 sets (train, validation and test)?

I have a pandas dataframe and I wish to divide it to 3 separate sets. I know that using train_test_split from sklearn.cross_validation, one can divide the data in two sets (train and test). However, I couldn't find any solution about splitting the…
CentAu
  • 10,660
  • 15
  • 59
  • 85
244
votes
12 answers

Concatenate a NumPy array to another NumPy array

I have a numpy_array. Something like [ a b c ]. And then I want to concatenate it with another NumPy array (just like we create a list of lists). How do we create a NumPy array containing NumPy arrays? I tried to do the following without any…
frazman
  • 32,081
  • 75
  • 184
  • 269
242
votes
6 answers

How to count the number of true elements in a NumPy bool array

I have a NumPy array 'boolarr' of boolean type. I want to count the number of elements whose values are True. Is there a NumPy or Python routine dedicated for this task? Or, do I need to iterate over the elements in my script?
norio
  • 3,652
  • 3
  • 25
  • 33
242
votes
10 answers

Numpy - add row to array

How does one add rows to a numpy array? I have an array A: A = array([[0, 1, 2], [0, 2, 0]]) I wish to add rows to this array from another array X if the first element of each row in X meets a specific condition. Numpy arrays do not have a method…
Darren J. Fitzpatrick
  • 7,159
  • 14
  • 45
  • 49
237
votes
8 answers

How do you get the magnitude of a vector in Numpy?

In keeping with the "There's only one obvious way to do it", how do you get the magnitude of a vector (1D array) in Numpy? def mag(x): return math.sqrt(sum(i**2 for i in x)) The above works, but I cannot believe that I must specify such a…
Nick T
  • 25,754
  • 12
  • 83
  • 121
237
votes
5 answers

How to flatten only some dimensions of a numpy array

Is there a quick way to "sub-flatten" or flatten only some of the first dimensions in a numpy array? For example, given a numpy array of dimensions (50,100,25), the resultant dimensions would be (5000,25)
IssamLaradji
  • 6,637
  • 8
  • 43
  • 68
237
votes
4 answers

How do I catch a numpy warning like it's an exception (not just for testing)?

I have to make a Lagrange polynomial in Python for a project I'm doing. I'm doing a barycentric style one to avoid using an explicit for-loop as opposed to a Newton's divided difference style one. The problem I have is that I need to catch a…
John K.
  • 2,473
  • 2
  • 13
  • 6
235
votes
4 answers

Suppress Scientific Notation in Numpy When Creating Array From Nested List

I have a nested Python list that looks like the following: my_list = [[3.74, 5162, 13683628846.64, 12783387559.86, 1.81], [9.55, 116, 189688622.37, 260332262.0, 1.97], [2.2, 768, 6004865.13, 5759960.98, 1.21], [3.74, 4062, 3263822121.39,…
donopj2
  • 3,933
  • 5
  • 23
  • 30
235
votes
1 answer

What is the inverse function of zip in python?

I've used the zip function from the Numpy library to sort tuples and now I have a list containing all the tuples. I had since modified that list and now I would like to restore the tuples so I can use my data. How can I do this?
user17151
  • 2,607
  • 3
  • 18
  • 13
233
votes
9 answers

How to install python modules without root access?

I'm taking some university classes and have been given an 'instructional account', which is a school account I can ssh into to do work. I want to run my computationally intensive Numpy, matplotlib, scipy code on that machine, but I cannot install…
Rishi
  • 3,538
  • 5
  • 29
  • 40