Python for Data Science: PartIII
Python is a powerful programming language used extensively in the field of data science. It has become the go-to language for data scientists because of its simplicity, flexibility, and the availability of various data science libraries. It is one of the top programming skills sought by employers. This is a quick tutorial for Python for Data Science.
Read Part I:Introduction to Python, Part II: Pyhon Basics
NumPy for Data Science
NumPy is a popular Python library used for scientific computing and data analysis. It provides support for large, multi-dimensional arrays and matrices, as well as a wide range of mathematical functions to operate on them.
In this tutorial, we will cover some of the essential NumPy basics, including creating and manipulating arrays, array operations and broadcasting, and linear algebra with NumPy.
Introduction to NumPy
NumPy is a third-party library for Python that provides support for large, multi-dimensional arrays and matrices. NumPy arrays are more efficient than Python lists, as they use less memory and allow for faster computations. NumPy also provides a wide range of mathematical functions that can be used to operate on arrays.
To use NumPy in your Python programs, you first need to install it using a package manager like pip or conda. Once installed, you can import NumPy using the following code:
pimport numpy as npIn the example above, we have imported NumPy and given it the alias “np”, which is a common convention in the Python community.
Creating and Manipulating Arrays
In NumPy, arrays can be created in various ways, such as using the “array” function, the “zeros” function, and the “ones” function. Here are some examples:
import numpy as np# Create an array from a list
a = np.array([1, 2, 3])
# Create an array of zeros
b = np.zeros((3, 3))
# Create an array of ones
c = np.ones((2, 2))
# Create a random array
d = np.random.rand(2, 2)In the example above, we have created four arrays using different NumPy functions.
Get Serawork Wallelign’s stories in your inbox
Join Medium for free to get updates from this writer.
Arrays in NumPy can be manipulated in various ways, such as reshaping, slicing, and indexing. Here are some examples:
import numpy as np# Create a 2D array
a = np.array([[1, 2, 3], [4, 5, 6]])
# Reshape the array
b = a.reshape((3, 2))
# Slice the array
c = a[:, 1:]
# Index the array
d = a[0, 2]In the example above, we have created a 2D array “a” and manipulated it using the “reshape”, “slice”, and “index” operations.
Array Operations and Broadcasting
NumPy provides a wide range of mathematical functions that can be used to operate on arrays. These functions include addition, subtraction, multiplication, division, and more. In NumPy, these operations can be performed element-wise on arrays of the same shape, using what is called “array broadcasting”.
Here are some examples:
import numpy as np# Create two arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Add the arrays
c = a + b
# Subtract the arrays
d = a - b
# Multiply the arrays
e = a * b
# Divide the arrays
f = a / bIn the example above, we have created two arrays “a” and “b” and performed element-wise operations on them using array broadcasting.
Linear Algebra with NumPy
NumPy provides support for linear algebra operations, such as matrix multiplication, determinant calculation, and eigendecomposition. Here are some examples:
import numpy as np# Create two matrices
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])Multiply the matrices
c = np.dot(a, b)
# Calculate the determinant of a matrix
d = np.linalg.det(a)
# Calculate the eigenvectors and eigenvalues of a matrix
eig_vals, eig_vecs = np.linalg.eig(a)In the example above, we have performed matrix multiplication, determinant calculation, and eigendecomposition using NumPy functions.
Conclusion
In this tutorial, we have covered some of the essential NumPy basics, including creating and manipulating arrays, array operations and broadcasting, and linear algebra with NumPy. NumPy provides a powerful toolset for scientific computing and data analysis in Python, and these basics will help you get started with using it in your own programs. With NumPy, you can easily work with large, multi-dimensional arrays and perform complex mathematical operations on them, making it an essential library for any data scientist or machine learning practitioner.






