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

Load compressed data (.npz) from file using numpy.load

I have an array: >>> data = np.ones((1,3,128)) I save it to file using savez_compressed: >>> with open('afile','w') as f: np.savez_compressed(f,data=data) When I try to load it I don't seem to be able to access the data: >>> with…
Lee
  • 29,398
  • 28
  • 117
  • 170
29
votes
2 answers

Is this the best way to add an extra dimension to a numpy array in one line of code?

If k is an numpy array of an arbitrary shape, so k.shape = (s1, s2, s3, ..., sn), and I want to reshape it so that k.shape becomes (s1, s2, ..., sn, 1), is this the best way to do it in one line? k.reshape(*(list(k.shape) + [1])
qAp
  • 1,139
  • 2
  • 12
  • 26
29
votes
7 answers

Efficiently Read last 'n' rows of CSV into DataFrame

A few methods to do this: Read the entire CSV and then use df.tail Somehow reverse the file (whats the best way to do this for large files?) and then use nrows argument to read Somehow find the number of rows in the CSV, then use skiprows and read…
Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
29
votes
2 answers

Python Numpy Data Types Performance

So I did some testing and got odd results. Code: import numpy as np import timeit setup = """ import numpy as np A = np.ones((1000,1000,3), dtype=datatype) """ datatypes = "np.uint8", "np.uint16", "np.uint32", "np.uint64", "np.float16",…
Gonzo
  • 2,023
  • 3
  • 21
  • 30
29
votes
5 answers

How to return a view of several columns in numpy structured array

I can see several columns (fields) at once in a numpy structured array by indexing with a list of the field names, for example import numpy as np a = np.array([(1.5, 2.5, (1.0,2.0)), (3.,4.,(4.,5.)), (1.,3.,(2.,6.))], dtype=[('x',float),…
askewchan
  • 45,161
  • 17
  • 118
  • 134
29
votes
7 answers

Compare (assert equality of) two complex data structures containing numpy arrays in unittest

I use Python's unittest module and want to check if two complex data structures are equal. The objects can be lists of dicts with all sorts of values: numbers, strings, Python containers (lists/tuples/dicts) and numpy arrays. The latter are the…
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
29
votes
1 answer

Vectorized look-up of values in Pandas dataframe

I have two pandas dataframes one called orders and another one called daily_prices. daily_prices is as follows: AAPL GOOG IBM XOM 2011-01-10 339.44 614.21 142.78 71.57 2011-01-13 342.64 616.69 143.92 73.08 2011-01-26 …
luckyfool
  • 1,653
  • 3
  • 14
  • 12
29
votes
15 answers

gotchas where Numpy differs from straight python?

Folks, is there a collection of gotchas where Numpy differs from python, points that have puzzled and cost time ? "The horror of that moment I shall never never forget !" "You will, though," the Queen said, "if you don't make a memorandum of…
denis
  • 21,378
  • 10
  • 65
  • 88
29
votes
8 answers

Numpy Indexing: Return the rest

A simply example of numpy indexing: In: a = numpy.arange(10) In: sel_id = numpy.arange(5) In: a[sel_id] Out: array([0,1,2,3,4]) How do I return the rest of the array that are not indexed by sel_id? What I can think of is: In: numpy.array([x for x…
CJLam
  • 801
  • 3
  • 10
  • 15
29
votes
2 answers

ValueError: Dimension mismatch

I use SciPy and scikit-learn to train and apply a Multinomial Naive Bayes Classifier for binary text classification. Precisely, I use the module sklearn.feature_extraction.text.CountVectorizer for creating sparse matrices that hold word feature…
pemistahl
  • 9,304
  • 8
  • 45
  • 75
29
votes
2 answers

Creating a 3D plot from a 3D numpy array

Ok, so I feel like there should be an easy way to create a 3-dimensional scatter plot using matplotlib. I have a 3D numpy array (dset) with 0's where I don't want a point and 1's where I do, basically to plot it now I have to step through three for:…
pter
  • 611
  • 2
  • 9
  • 16
29
votes
5 answers

how to return numpy.array from boost::python?

I would like to return some data from c++ code as a numpy.array object. I had a look at boost::python::numeric, but its documentation is very terse. Can I get an example of e.g. returning a (not very large) vector to python? I don't mind…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
29
votes
3 answers

Python : How to avoid numpy RuntimeWarning in function definition?

i designed a simple function to return a mathematical function which can be used to fit experimental data to it. The functions looks pretty much like the following: def colecole_2(f,*p): term1=p[0] * ( 1 - 1 / (1 + numpy.power((0+1j) * 2 *…
user1371788
28
votes
3 answers

Is there an "enhanced" numpy/scipy dot method?

Problem I would like to compute the following using numpy or scipy: Y = A**T * Q * A where A is a m x n matrix, A**T is the transpose of A and Q is an m x m diagonal matrix. Since Q is a diagonal matrix I store only its diagonal elements as a…
Woltan
  • 13,723
  • 15
  • 78
  • 104
28
votes
10 answers

Java equivalent for the Numpy multi-dimensional object

After using it for a while, I really like the Numpy multi-dimensional array. It's helpful to write algorithms with a concise yet readable and fairly general code. I wish to have the same thing in Java. Before coding a multi-dimensional array with a…
Monkey
  • 1,838
  • 1
  • 17
  • 24
1 2 3
99
100