NumPy
- stands for Numerical Python
- Designed for scientific computing and analysis
- Deals with multidimensional arrays and matrices.
- Includes a large collection of high level mathematical functions
- Faster than standard python packages
Applications
- Images
- Audio
- Measurements
- Numbers
- Libraries
- pandas
- SciPy
- OpenCV
pip install numpy
pip3 install numpy
NumPy Arrays
- Main data structure in Numpy
- can be single of mutiudimensional
- Contains elements of a single type
Numpy Arrays versus Python List
- No loop structure required for iteration
- Allows only one type per array
- less flexible
- makes type checking unnecessary
- faster performance
NumPy Array Efficiency
- Memory usage is much smaller with arrays
- In lists, each item is pointer (occupying between 4 to 16 bytes)
- NumPy arryas are contiguous blocks of memory, so no need for pointers
- all items are fixed size and of one type
np . array Constructor
np.array(object, dtype)
# return a new NumPy array from a given object
# object can be any array like object
# dtype is the type of the array
# if not specified, it will be set automatically based on contents of the object
Integer Types
np.bool | boolean |
np.int8 | byte (-128-127) |
np.int16 | 16bit-signed-integere (-2hoch15 to 2 hoch 15 -1) |
np.int32 | 32bit-signed-integere (-2hoch31 to 2 hoch 31 -1) |
np.int64 | 64bit-signed-integere (-2hoch63 to 2 hoch 63 -1) |
Unsigned Types
np.uint8 | byte (0 - 256) |
np.uint16 | 16bit-signed-integere (0 to 2 hoch 16) |
np.uint32 | 32bit-signed-integere (0 to 2 hoch 32) |
np.uint64 | 64bit-signed-integere (0 to 2 hoch 64) |
Float/Complex Types
np.float32 | float |
np.float64 | double |
np.complex64 | float complex |
np.complex128 | double complex |
One Dimensional Array Creation
a1 = np.array([1.2,32.4,7.5,3.4])
# one dimensional array from a list of floats
# np.array selcts a type based on the input we have given it
Two Dimensional Array Creation
a2 = np.array([2,4,10,15,13], [5,3,2,13,7],dtype=np.int16 )
# the argument is a list of lists, and the list of lists has two lists, so it makes a
# two dimensional array
Array Population Methods
- Fill an array with:
- zeros, ones or any specific number
- a range of numbers
- a range of random numbers
Operators
- Operations can be performed on scalars or arrays with the same shape
- Arithmetic operators (+- * ** / // %)
- Comparison Operators (< <= == != => >)
Arithmetic Operators Example
a = np.array([2,10,-4, 5, 3.7, 80])
print(a + 2)
print(a * 10-5)
# Output [ 4. 12. -2. 7. 5.7 82. ]
[ 15. 95. -45. 45. 32. 795. ]
# we are not using a for loop
Comparison Operators Example
- comparing an array with a scalar
a = np.array([2, 10, -4, 5, 3.7, 80])
print(a == 10)
print(a >= 5)
# Output [ False True False False False False ]
[ False True False True False True ]
# we are not using a for loop
Other Methods
- sum, product
- minimum, maximum
- Median, average, mean and standard deviaation
- Histogram
Recommendations
- numpy.org
- interactive shell to try NumPy in the browser
- matplotlib.org
- creating static, animated, and interactive visualizations
- has tons of Plot types (in py or Jupyter)
- pandas.pydata.org
pip install pandas
conda install -c conda-forge pandas
- user guide
- SQL tutorial