NumPy is a Python library that provides support for large, multi-dimensional arrays and
matrices, along with a wide range of high-performance mathematical functions to operate on
these arrays. It is often used in conjunction with other popular libraries like Pandas, Matplotlib,
and Scikit-learn to perform data analysis, machine learning, and visualization tasks.
NumPy Methods
NumPy provides a wide range of methods for performing various operations on arrays.
Here are some examples:
1. Basic Operations:
np.sum(): Calculate the sum of an array.
np.mean(): Calculate the mean of an array.
np.median() : Calculate the median of an array.
np.std(): Calculate the standard deviation of an array.
2. Indexing and Slicing:
my_array[0] : Access the first element of the array.
my_array[1:3] : Access the second and third elements of the array.
my_array[:, 1]: Access the second column of a 2D array.
3. Array Manipulation:
np.concatenate(): Concatenate two or more arrays.
np.reshape(): Reshape an array to a new shape.
np.transpose(): Transpose an array (swap rows and columns).
4. Mathematical Operations:
np.dot(): Perform matrix multiplication.
np.linalg.inv() : Calculate the inverse of a matrix.
np.linalg.det() : Calculate the determinant of a matrix.
Here is a comprehensive NumPy cheat sheet with many of its methods, along
with examples and explanations. This file covers various aspects of NumPy,
including array creation, basic operations, indexing and slicing, array
manipulation, mathematical operations, random number generation, and
more.
NumPy Cheat Sheet
Array Creation
numpy.array(): Create a NumPy array from a Python list or tuple.
python
VerifyOpen In EditorEditCopy code
1import numpy as np
2my_list = [1, 2, 3, 4, 5]
3my_array = np.array(my_list)
numpy.arange(): Create a NumPy array with a specified range of values.
python
VerifyOpen In EditorEditCopy code
1my_array = np.arange(10) # [0, 1, 2, ..., 9]
numpy.linspace(): Create a NumPy array with a specified range of values
and a specified number of elements.
python
VerifyOpen In EditorEditCopy code
1my_array = np.linspace(0, 10, 5) # [0, 2.5, 5, 7.5, 10]
numpy.random.rand(): Create a NumPy array with random values.
python
VerifyOpen In EditorEditCopy code
1my_array = np.random.rand(3, 4) # 3x4 array with random values between 0 and 1
numpy.random.normal(): Create a NumPy array with random values from a
normal distribution.
python
VerifyOpen In EditorEditCopy code
1my_array = np.random.normal(loc=0, scale=1, size=(3, 4)) # 3x4 array with random
values from a normal distribution
numpy.zeros(): Create a NumPy array with all elements set to zero.
python
VerifyOpen In EditorEditCopy code
1my_array = np.zeros((3, 4)) # 3x4 array with all elements set to zero
numpy.ones(): Create a NumPy array with all elements set to one.
python
VerifyOpen In EditorEditCopy code
1my_array = np.ones((3, 4)) # 3x4 array with all elements set to one
Basic Operations
numpy.sum(): Calculate the sum of an array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([1, 2, 3, 4, 5])
2result = np.sum(my_array) # 15
numpy.mean(): Calculate the mean of an array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([1, 2, 3, 4, 5])
2result = np.mean(my_array) # 3.0
numpy.median(): Calculate the median of an array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([1, 2, 3, 4, 5])
2result = np.median(my_array) # 3.0
numpy.std(): Calculate the standard deviation of an array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([1, 2, 3, 4, 5])
2result = np.std(my_array) # 1.58113883046
numpy.min(): Calculate the minimum value of an array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([1, 2, 3, 4, 5])
2result = np.min(my_array) # 1
numpy.max(): Calculate the maximum value of an array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([1, 2, 3, 4, 5])
2result = np.max(my_array) # 5
Indexing and Slicing
my_array[0]: Access the first element of the array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([1, 2, 3, 4, 5])
2result = my_array[0] # 1
my_array[1:3]: Access the second and third elements of the array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([1, 2, 3, 4, 5])
2result = my_array[1:3] # [2, 3]
my_array[:, 1]: Access the second column of a 2D array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([[1, 2], [3, 4], [5, 6]])
2result = my_array[:, 1] # [2, 4, 6]
my_array[1:, 1:]: Access the second row and second column of a 2D
array.
python
VerifyOpen In EditorEditCopy code
1my_array = np.array([[1, 2], [3, 4], [5, 6]])
2result = my_array[1:, 1:] # [[4], [6]]
Array Manipulation
numpy.concatenate(): Concatenate two