NumPy: Understanding NumPy python library

NUMPY NumPy is the core library for scientific computing in Python. Foundational Python libraries such as pandas, SciPy, and Matplotlib are built on top of NumPy's API. So are machine learning libraries such as TensorFlow and scikit-learn, which use NumPy arrays as inputs. NumPy arrays The array is the main object in NumPy; it's a grid-like structure that holds data. An array can have any number of dimensions, and each dimension can be any length. Creating 1D arrays from lists We can create arrays from Python lists by passing a list as an argument to the np.array function. The data type of this array is a NumPy nd.array, or n-dimensional array. python_list =[3,2,5,9,7,1,4,3,6] array = np.array(python_list) array array([3,2,5,9,7,1,4,3,6) Creating 2D arrays from lists To create a 2-dimensional array, pass np.array a list of lists. A list of lists of lists would generate a 3-dimensional array. python_list_of_lists = ([[3,2,5], [9,7,1], [4,3,6]]) Arrays vs. Python lists *Python lists can include many different data types: python_list = ['beep', False, 56, .924, [3,2,5]] NumPy array must be the same data type. This makes NumPy very efficient: there's no need for NumPy to check the data type of each element in an array since they must all be the same. Having only a single data type also means that a NumPy array takes up less space in memory than the same information would if stored as a Python list. numpy_boolean_array = [[True, False], [True, True], [False, True]] numpy_float_array = [1.9, 2.4, 3.1, 6.4] Creating arrays from scratch There are many Numpy functions used to create arrays. Using functions such as np.zeros() np.random.random() np.arange() Creating arrays: np.zeros() np.zeros() creates an array full of zeros. We can use the zeros array just as we might use an empty Python list, filling it with data later on. We tell np.zeros() the shape of the desired array using a tuple of integers, each representing the length of a given dimension. A tuple is a Python data type used to store collections of data, similar to a list. Tuples are created with parentheses() Here, when np.zeros() is given a tuple of five and three, it creates an array with five rows and three columns. np.zeros((5,3)) array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]. [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) Creating arrays: np.random.random() np.random.random() also accepts a tuple with the desired array's shape. The array will be made up of random floats between 0 - 1 np.random.random((2,4)) array([[0.8852461, 0.08564132, 0.33463107, 0.53337117], [0.69933362, 0.09295327, 0.93617865, 0.04532187]]) np.random.random() is called that, because np.random.random() is a function within NumPy's random module, which contains useful functions for random sampling. np.random. {NumPy module} random() {function name} Creating arrays: np.arange() np.arange() creates an evenly-spaced array of numbers based on given start and stop values. By default, np.arange() creates an array of sequential integers. The start value is included in the output array, but the stop value is not. The start value can be omitted if the range begins with zero. If a third argument is passed, it is interpreted as the step value. Now, the desired distance between elements is three. *np.arange() is particularly useful for plotting! np.arange(-3,4) array([-3, -2, -2, 0, 1, 2, 3]) np.arange(4) array([0, 1, 2, 3]) np.arange(-3, 4, 3) array([-3, 0, 3])

Mar 24, 2025 - 16:59
 0
NumPy: Understanding NumPy python library

NUMPY

NumPy is the core library for scientific computing in Python.

  • Foundational Python libraries such as pandas, SciPy, and Matplotlib are built on top of NumPy's API. So are machine learning libraries such as TensorFlow and scikit-learn, which use NumPy arrays as inputs.

NumPy arrays

The array is the main object in NumPy; it's a grid-like structure that holds data. An array can have any number of dimensions, and each dimension can be any length.

Creating 1D arrays from lists

  • We can create arrays from Python lists by passing a list as an argument to the np.array function.
  • The data type of this array is a NumPy nd.array, or n-dimensional array.
python_list =[3,2,5,9,7,1,4,3,6]
array = np.array(python_list)

array

array([3,2,5,9,7,1,4,3,6)

Creating 2D arrays from lists

  • To create a 2-dimensional array, pass np.array a list of lists. A list of lists of lists would generate a 3-dimensional array.
python_list_of_lists = ([[3,2,5],
                        [9,7,1],
                        [4,3,6]])

Arrays vs. Python lists

*Python lists can include many different data types:

python_list = ['beep', False, 56, .924, [3,2,5]]

  • NumPy array must be the same data type.
  • This makes NumPy very efficient: there's no need for NumPy to check the data type of each element in an array since they must all be the same.
  • Having only a single data type also means that a NumPy array takes up less space in memory than the same information would if stored as a Python list.
numpy_boolean_array = [[True, False], [True, True], [False, True]]
numpy_float_array = [1.9, 2.4, 3.1, 6.4]

Creating arrays from scratch

  • There are many Numpy functions used to create arrays. Using functions such as
    • np.zeros()
    • np.random.random()
    • np.arange()

Creating arrays: np.zeros()

  • np.zeros() creates an array full of zeros. We can use the zeros array just as we might use an empty Python list, filling it with data later on. We tell np.zeros() the shape of the desired array using a tuple of integers, each representing the length of a given dimension.
  • A tuple is a Python data type used to store collections of data, similar to a list. Tuples are created with parentheses()
  • Here, when np.zeros() is given a tuple of five and three, it creates an array with five rows and three columns.
np.zeros((5,3))

array([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.].
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])

Creating arrays: np.random.random()

  • np.random.random() also accepts a tuple with the desired array's shape. The array will be made up of random floats between 0 - 1
 np.random.random((2,4))

 array([[0.8852461, 0.08564132, 0.33463107, 0.53337117],
        [0.69933362, 0.09295327, 0.93617865, 0.04532187]])
  • np.random.random() is called that, because np.random.random() is a function within NumPy's random module, which contains useful functions for random sampling.

np.random. {NumPy module}

random() {function name}

Creating arrays: np.arange()

  • np.arange() creates an evenly-spaced array of numbers based on given start and stop values. By default, np.arange() creates an array of sequential integers. The start value is included in the output array, but the stop value is not. The start value can be omitted if the range begins with zero. If a third argument is passed, it is interpreted as the step value. Now, the desired distance between elements is three. *np.arange() is particularly useful for plotting!
np.arange(-3,4)
array([-3, -2, -2, 0, 1, 2, 3])

np.arange(4)
array([0, 1, 2, 3])

np.arange(-3, 4, 3)
array([-3, 0, 3])