NumPy is the fundamental package for scientific computing with Python.
It contains among other things:
- a powerful N-dimensional array object
- sophisticated (broadcasting) functions
- tools for integrating C/C++ and Fortran code
- useful linear algebra, Fourier transform, and random number capabilities
- Numeric Python
- Alternative to Python List: Numpy Array
- Calculations over entire arrays
- Easy and Fast
- In the terminal:
pip3 install numpy
python3 -m pip install numpy
In : import numpy as np In : np_height = np.array(height) In : np_height Out: array([ 1.73, 1.68, 1.71, 1.89, 1.79]) In : np_weight = np.array(weight) In : np_weight Out: array([ 65.4, 59.2, 63.6, 88.4, 68.7]) In : bmi = np_weight / np_height ** 2 In : bmi Out: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
First we imported the numpy package as np then, we created a new numpy array using “np.array(LIST_NAME)”
Element Wise Calculation
In : height = [1.73, 1.68, 1.71, 1.89, 1.79] In : weight = [65.4, 59.2, 63.6, 88.4, 68.7] In : weight / height ** 2 TypeError: unsupported operand type(s) for **: 'list' and 'int' In : np_height = np.array(height) In : np_weight = np.array(weight) In : np_weight / np_height ** 2 Out: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
- Numpy arrays: contain only one type
- Different types: different behavior!
In : np.array([1.0, "is", True]) Out: array(['1.0', 'is', 'True'], dtype='') # Numpy arrays: contain only one type # Different types: different behavior In : python_list = [1, 2, 3] In : numpy_array = np.array([1, 2, 3]) In : python_list + python_list Out: [1, 2, 3, 1, 2, 3] In : numpy_array + numpy_array Out: array([2, 4, 6])
Numpy Arrays can only store values of one datatype, if you insert more then one type values, then the complete array will be converted in the highest precise datatype, most of the time, it is string
You can with regular Python lists. When you want to get elements from your array, for example, you can again use square brackets. Suppose you want to get the
bmi for the second person, so at index 1.
This will od the trick: Specifically for Numpy, there’s also another way to do list subsetting: using an array of booleans.
Say you want to get all BMI values in the bmi array that are over 23.
A first step is using the greater than sign, like this: The result is a Numpy array containing booleans: True if the corresponding bmi is above 23, False if it’s below. Next, you can use this boolean array inside square brackets to dosubsetting. Only the elements in
bmi that are above 23, so for which the corresponding boolean value is True, is selected. There’s only one BMI that’s above 23, so we end up with a Numpy array with a single value, that BMI. Using the result of a comparison to make a selection of your data is a very common way to get surprising insights
In : bmi Out: array([ 21.852, 20.975, 21.75 , 24.747, 21.441]) In : bmi Out: 20.975 In : bmi > 23 Out: array([False, False, False, True, False], dtype=bool) In : bmi[bmi > 23] Out: array([ 24.747])