Python Programming/numpy
Numpy is a numeric library for Python. Its main purpose is to provide fast manipulation of multidimensional arrays.
Installation
[edit | edit source]It's provided with the main Linux distribution, however it can be installed through the Debian package python-numpy. On systems with pip, running pip install numpy will work. On Windows, it can be downloaded on http://sourceforge.net/projects/numpy/files/.
Then, once the .zip unpacked, the installation is done by entering into the console:
python setup.py install
In case of error:
- ImportError “No Module named Setuptools”, save the file https://bootstrap.pypa.io/ez_setup.py into the Python folder, and install it with:
python ez_setup.py install.- Microsoft Visual C++ 9.0 is required, download it on https://www.microsoft.com/en-us/download/details.aspx?id=44266
Arrays
[edit | edit source]Arrays in numpy are of type numpy.ndarray. However, the most common way of defining them is using numpy.array, which is a function. For example,
import numpy
a = numpy.array([1, 2, 3])
b = numpy.array([[3, 1, 4, 1, 5],
[9, 2, 6, 5, 3]])
There are other ways to initialize numpy arrays. For example, to initialize a random array,
import numpy
rng = numpy.random.default_rng() # initialize random number generator
a = rng.random(5) # five random floats between 0 and 1
b = rng.random(3, 5) # a 3 by 5 array of floats between 0 and 1
c = rng.integers(0, 10, size=3) # 3 random integers between 0 and 10, excluding 10 and including 0
Some other ways of initialization are:
import numpy
a = numpy.zeros(2, 3) # 2 by 3 array of only zeros.
b = numpy.ones(3, 5) # 3 by 5 array of only ones.
Histogram
[edit | edit source]import numpy
mydata = [numpy.random.normal(0,1) for i in range(10000) ]
h, n = numpy.histogram( mydata , 100, (-5,5) )
This section is a stub. You can help Wikibooks by expanding it. |