NumPy (Part 1): Introduction to NumPy arrays
Following our Introduction to Data Science in Python, this article introduces NumPy (Part 1): Introduction to NumPy arrays. NumPy is the fundamental package for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays.
📚 Prerequisites
- Basic understanding of Python lists.
🎯 Article Outline: What You'll Master
- ✅ Installation: How to install NumPy.
- ✅ Core Concepts: What a NumPy array is and how it differs from a Python list.
- ✅ Creating Arrays: Different ways to create NumPy arrays.
- ✅ Array Attributes: Understanding the shape, size, and data type of arrays.
🧠 Section 1: The Core Concepts of NumPy Arrays
A NumPy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.
NumPy arrays vs. Python lists:
- Performance: NumPy arrays are much faster and more memory-efficient than Python lists.
- Homogeneous: All elements in a NumPy array must be of the same data type.
- Functionality: NumPy provides a large number of functions that operate on arrays.
💻 Section 2: Deep Dive - Implementation and Walkthrough
2.1 - Installation
pip install numpy
2.2 - Creating NumPy Arrays
import numpy as np
# Create a 1D array from a list
a = np.array([1, 2, 3])
# Create a 2D array
b = np.array([[1, 2, 3], [4, 5, 6]])
# Create an array of zeros
c = np.zeros((2, 3))
# Create an array of ones
d = np.ones((3, 2))
# Create an array with a range of elements
e = np.arange(10, 25, 5)
# Create an array of random values
f = np.random.random((2, 2))
2.3 - Array Attributes
# Shape of the array
print(b.shape) # (2, 3)
# Number of dimensions
print(b.ndim) # 2
# Number of elements
print(b.size) # 6
# Data type of elements
print(b.dtype) # int64
💡 Conclusion & Key Takeaways
You've learned the basics of NumPy arrays, including how to create them and how to inspect their attributes.
Let's summarize the key takeaways:
- NumPy arrays are the foundation of numerical computing in Python.
- They are faster and more memory-efficient than Python lists.
- You can create arrays in various ways, including from lists, with zeros or ones, or with random values.
➡️ Next Steps
In the next article, "NumPy (Part 2): Array indexing, slicing, and operations", we will explore how to work with the data inside NumPy arrays.
Glossary
- NumPy: A Python library for numerical computing.
- Array: A grid of values, all of the same type.
- Rank: The number of dimensions of an array.
- Shape: The size of the array along each dimension.