Some key features of NumPy include:
- Arrays: NumPy provides a powerful
array
object that represents arrays and matrices. These can be of any dimension and can contain elements of the same data type. - Mathematical Functions: NumPy comes with a wide range of mathematical functions that can be applied to arrays, making it efficient for numerical operations.
- Broadcasting: NumPy supports broadcasting, allowing operations on arrays of different shapes and sizes.
- Linear Algebra Operations: NumPy includes functions for linear algebra operations, such as matrix multiplication, eigenvalues, and solving linear systems.
- Random Number Generation: NumPy provides tools for generating random numbers and statistical distributions.
To use NumPy in Python, you typically start by importing the library:
# The first step of using numpy is to tell python to import it
import numpy as np
After importing, you can create arrays, perform mathematical operations, and leverage the various functionalities provided by NumPy.
NumPy Arrays
sports = ['Cricket', 'Football', 'Tennis', 'Golf', 'Baseball']
sports_new = np.array(sports)
print(sports_new)
['Cricket' 'Football' 'Tennis' 'Golf' 'Baseball']
After converting a Python list or matrix to NumPy array, when we print it, there is no ',' between the values.
NumPy Functions
np.arange
np.arange
is a function in the NumPy library that creates an array of evenly spaced values within a specified range. The syntax for np.arange
is as follows:
numpy.arange([start, ]stop, [step, ], dtype=None)
start
: The starting value of the sequence (optional, default is 0).stop
: The end value of the sequence (exclusive).step
: The step size between values (optional, default is 1).dtype
: The data type of the output array (optional).
import numpy as np
# Example 1: Default start and step
arr1 = np.arange(5) # Creates an array [0, 1, 2, 3, 4]
# Example 2: Specify start, stop, and step
arr2 = np.arange(2, 10, 2) # Creates an array [2, 4, 6, 8]
# Example 3: Specify dtype
arr3 = np.arange(0, 1, 0.1, dtype=float) # Creates an array [0.0, 0.1, 0.2, ..., 0.9]
# Example 4: Negative step
arr4 = np.arange(5, 0, -1) # Creates an array [5, 4, 3, 2, 1]
np.linspace
np.linspace
is another function in the NumPy library that creates an array of evenly spaced values over a specified range. The key difference between np.arange
and np.linspace
lies in how the spacing is defined. The syntax for np.linspace
as follows:
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
start
: The starting value of the sequence.stop
: The end value of the sequence.num
: The number of evenly spaced values to generate (default is 50).endpoint
: If True (default),stop
is the last value in the range; if False, it is not included.retstep
: If True, return the step size between values (default is False).dtype
: The data type of the output array (optional).
Here are a few examples:
import numpy as np
# Example 1: Default values
arr1 = np.linspace(0, 1) # Creates an array with 50 values from 0 to 1
# Example 2: Specify the number of values
arr2 = np.linspace(0, 10, num=5) # Creates an array with 5 values from 0 to 10
# Example 3: Exclude endpoint
arr3 = np.linspace(0, 5, num=10, endpoint=False) # Creates an array with 10 values from 0 to 5, excluding 5
# Example 4: Return step size
arr4, step_size = np.linspace(1, 10, num=5, retstep=True) # Creates an array with 5 values from 1 to 10 and returns the step size
Unlike np.arange
, np.linspace
includes both the start
and stop
values by default. It’s commonly used when you need a specific number of evenly spaced values within a range, and the exact step size is important
np.zeros
np.zeros
is a function in the NumPy library that creates an array filled with zeros. The syntax for np.zeros
is as follows:
numpy.zeros(shape, dtype=float, order='C')
shape
: The shape of the array, i.e., the number of elements along each dimension. It can be an integer or a tuple of integers specifying the dimensions.dtype
: The data type of the array (optional, default isfloat
).order
: Specifies whether to store multi-dimensional data in row-major (‘C’) or column-major (‘F’) order (optional, default is ‘C’).
Here are a few examples:
import numpy as np
# Example 1: 1D array with 5 zeros
arr1 = np.zeros(5) # Creates an array [0., 0., 0., 0., 0.]
# Example 2: 2D array with shape (2, 3)
arr2 = np.zeros((2, 3)) # Creates a 2x3 array filled with zeros
print(arr2)
[[0. 0. 0.]
[0. 0. 0.]]
# Example 3: 3D array with shape (2, 2, 2)
arr3 = np.zeros((2, 2, 2)) # Creates a 2x2x2 array filled with zeros
print(arr3)
[[[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]]]
You can specify the shape of the array using the shape
parameter, and the default data type is float
. The resulting array will have all its elements initialized to zero.
np.zeros
is useful when you want to create an array of a specific shape and initialize all its elements to zero. It is commonly used in various numerical and scientific computing applications.
np.ones
np.ones
is a function in the NumPy library that creates an array filled with ones. The syntax for np.ones
is similar to np.zeros
:
numpy.ones(shape, dtype=None, order='C')
shape
: The shape of the array, i.e., the number of elements along each dimension. It can be an integer or a tuple of integers specifying the dimensions.dtype
: The data type of the array (optional, default isfloat
).order
: Specifies whether to store multi-dimensional data in row-major (‘C’) or column-major (‘F’) order (optional, default is ‘C’).
Here are a few examples:
import numpy as np
# Example 1: 1D array with 3 ones
arr1 = np.ones(3) # Creates an array [1., 1., 1.]
# Example 2: 2D array with shape (2, 4)
arr2 = np.ones((2, 4)) # Creates a 2x4 array filled with ones
print(arr2)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
# Example 3: 3D array with shape (3, 3, 3)
arr3 = np.ones((3, 3, 3)) # Creates a 3x3x3 array filled with ones
print(arr3)
[[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]]
You can specify the shape of the array using the shape
parameter, and the default data type is float
. The resulting array will have all its elements initialized to one.
np.ones
is useful when you want to create an array of a specific shape and initialize all its elements to one. It is commonly used in various numerical and scientific computing applications.
np.eye
np.eye
is a NumPy function that is used to create a 2-dimensional identity matrix or an identity array. An identity matrix is a square matrix in which all the elements of the principal diagonal are ones, and all other elements are zeros. The syntax for np.eye
is as follows:
numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C')
N
: The number of rows in the output array.M
: (Optional) The number of columns in the output array. If not specified, it defaults toN
.k
: (Optional) The index of the diagonal (main diagonal by default). Usek > 0
for diagonals above the main diagonal andk < 0
for diagonals below the main diagonal.dtype
: (Optional) Data-type of the output array. If not specified, it defaults tofloat
.order
: (Optional) Specifies whether to store multi-dimensional data in row-major (‘C’) or column-major (‘F’) order. Default is ‘C’.
Here are a few examples:
import numpy as np
# Example 1: 5x5 identity matrix
arr1 = np.eye(5)
print(arr1)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
# Example 2: 4x5 identity matrix
arr2 = np.eye(4, 5)
# Example 3: 3x3 identity matrix with diagonal offset by 1
arr3 = np.eye(3, k=1)
print(arr3)
[[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
In these examples, np.eye
is used to create identity matrices of different sizes and with optional parameters. Identity matrices are often used in linear algebra and various mathematical computations.
np.reshape
np.reshape
is a function in the NumPy library that is used to change the shape of an array without changing its data. It returns a new array with the same data but a different shape. The syntax for np.reshape
is as follows:
numpy.reshape(a, newshape, order='C')
a
: The array to be reshaped.newshape
: The new shape to which the array should be reshaped. It can be specified as an integer or a tuple of integers.order
: (Optional) Specifies whether to store multi-dimensional data in row-major (‘C’) or column-major (‘F’) order. Default is ‘C’.
Here are a few examples:
import numpy as np
# Example 1: Reshape a 1D array to a 2D array (3x4)
arr1 = np.arange(12).reshape((3, 4))
print(arr1)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
# Example 3: Reshape a 2D array (4x3) to a flattened 1D array
arr3 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
flattened_arr3 = arr3.reshape(-1)
print(flattened_arr3)
[ 1 2 3 4 5 6 7 8 9 10 11 12]
In these examples, np.reshape
is used to change the shape of arrays. The newshape
parameter can be specified as a tuple of integers indicating the desired dimensions of the reshaped array. If one of the dimensions is set to -1, NumPy will automatically calculate the size for that dimension based on the size of the original array.
np.reshape
is useful when you need to rearrange the dimensions of an array to fit a specific format or when working with arrays of different shapes in various numerical and scientific computing applications.
np.transpose
np.transpose
is a NumPy function that returns the transpose of an array. The transpose of an array is obtained by swapping the axes. For a 2-dimensional array, it is equivalent to swapping the rows and columns. The syntax for np.transpose
is as follows:
numpy.transpose(a, axes=None)
a
: The input array.axes
: (Optional) By default, reverse the dimensions, otherwise, permute the dimensions according to the specified axes.
Here are a few examples:
import numpy as np
# Example 1: Transpose a 2D array
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
transposed_arr1 = np.transpose(arr1)
print(transposed_arr1)
[[1 4]
[2 5]
[3 6]]
# Example 2: Transpose with specified axes
arr2 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
transposed_arr2 = np.transpose(arr2, axes=(0, 2, 1))
# Example 3: Transpose a 1D array (no effect)
arr3 = np.array([1, 2, 3])
transposed_arr3 = np.transpose(arr3)
print(arr3)
[1 2 3]
In these examples, np.transpose
is used to obtain the transpose of arrays. The axes
parameter allows you to specify the order of dimensions in the transposed array. If not specified, it reverses the order of dimensions by default.
Transposing arrays is commonly used in linear algebra, signal processing, and other numerical computations where rearranging dimensions is necessary.
np.random.rand
np.random
is a submodule of the NumPy library that provides functions for generating random numbers and random arrays. It is widely used in various applications, including simulations, statistical analysis, and machine learning. Here are some commonly used functions within np.random
:
- Random Numbers:
np.random.rand
: Generates random numbers from a uniform distribution over (0, 1).
random_numbers = np.random.rand(3, 4) # Generates a 3x4 array of random numbers
np.random.randn
: Generates random numbers from a standard normal distribution (mean=0, standard deviation=1).
random_normal_numbers = np.random.randn(2, 2) # Generates a 2x2 array of random normal numbers
- Random Integers:
np.random.randint
: Generates random integers from a specified low to high range.
random_integers = np.random.randint(1, 10, size=(3, 3)) # Generates a 3x3 array of random integers between 1 and 10
- Permutations:
np.random.shuffle
: Shuffles the elements of an array in place.
array_to_shuffle = np.array([1, 2, 3, 4, 5])
np.random.shuffle(array_to_shuffle) # Shuffles the array in place
np.random.permutation
: Returns a permuted range or a shuffled copy of an array.
permuted_array = np.random.permutation(array_to_shuffle) # Returns a shuffled copy of the array
- Random Sampling:
np.random.choice
: Generates a random sample from a given 1-dimensional array.
original_array = np.array([10, 20, 30, 40, 50])
random_sample = np.random.choice(original_array, size=3, replace=False) # Randomly samples 3 elements without replacement
These are just a few examples, and there are more functions and options available in np.random
for different types of random number generation and sampling. Keep in mind that if you need to generate reproducible random numbers, you can set a seed using np.random.seed
.