The Basics of Python: Numpy
An Introduction to NumPy: The Essential Python Library for Scientific Computing
NumPy is a Python library that is widely used for numerical and scientific computing. It provides a powerful array object and functions for working with these arrays.
Some of the most important functions in NumPy include:
np.array()
: creates a NumPy array from a Python list or tuplenp.zeros()
: creates an array filled with zerosnp.ones()
: creates an array filled with onesnp.arange()
: creates an array with a range of valuesnp.linspace()
: creates an array with a specific number of values evenly spaced between two endpointsnp.random.rand()
: creates an array with random values between 0 and 1np.random.randn()
: creates an array with random values from a normal distributionnp.min()
: returns the minimum value of an arraynp.max()
: returns the maximum value of an arraynp.mean()
: returns the mean of an arraynp.std()
: returns the standard deviation of an arraynp.sum()
: returns the sum of an array
Here's an example of how to create a NumPy array and use some of these functions:
import numpy as np
# create a 1-dimensional array from a Python list
my_array = np.array([1, 2, 3, 4, 5])
# create a 2-dimensional array filled with zeros
zeros_array = np.zeros((3, 4))
# create a 1-dimensional array with values from 0 to 9
range_array = np.arange(10)
# create a 1-dimensional array with 5 values between 0 and 1
linspace_array = np.linspace(0, 1, 5)
# create a 3x3 array with random values between 0 and 1
random_array = np.random.rand(3, 3)
# compute the minimum value of an array
min_value = np.min(my_array)
# compute the mean of an array
mean_value = np.mean(random_array)