Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Arrays in python?

Introduction to NumPy

A Beginner’s Guide to NumPy Arrays and Operations

15 min readJun 20, 2024

--

Press enter or click to view image in full size
Photo by Antoine Dautry on Unsplash

As an experienced developer with a passion for Python, I have seen firsthand how its versatility and simplicity can revolutionize data engineering. Python allows developers to streamline complex processes and enhance computational efficiency. I enjoy sharing my knowledge through insights on Python programming, covering everything from beginner tips to advanced techniques, as well as SQL, databases and AI advancements.

In this article, we’ll explore NumPy, a fundamental library for data manipulation and computational efficiency. Whether you’re a seasoned programmer or just starting out, this series will cover the basics, key features and practical examples to get you started with NumPy. Stay tuned for Part 2&3, where we’ll dive into more advanced functionalities and optimization techniques.

Welcome to the first part of our series on mastering NumPy to effectively, transform your data handling and computations.

Your engagement, whether through claps, comments, or following me, fuels my passion for creating and sharing more informative content.
If you’re interested in more
Python or data engineering content, please consider following me. Alternatively, you can click here to check out my Python list on Medium.

Introduction

NumPy, short for Numerical Python, is a fundamental package for scientific computing in Python. It is widely used for its powerful capabilities in handling large multi-dimensional arrays and matrices, along with a vast collection of mathematical functions to operate on these arrays. NumPy serves as the backbone for many scientific and numerical libraries, including Pandas, Matplotlib and SciPy, making it an essential tool for anyone working with data in Python.

Today, I aim to provide you with an in-depth understanding of NumPy’s core features and functionalities. Whether you’re a tech enthusiast looking to enhance your coding skills, an entrepreneur seeking to leverage data for business insights, or a member of the general public interested in learning about numerical computing, this article has something for everyone.

Here’s a brief overview of what I will cover in this article:

  • Getting Started with NumPy: Starting with the basics, including how to install NumPy and understand its primary concepts and terminologies.
  • Creating NumPy Arrays: Learn the various methods to create NumPy arrays, including from lists and using built-in NumPy functions.
  • Array Operations: Discover the power of NumPy’s array operations, including basic arithmetic operations and universal functions (ufuncs).
  • Indexing and Slicing: Master the techniques for accessing and modifying elements within an array.
  • Array Manipulation: Explore advanced array manipulation techniques such as reshaping, concatenation and splitting.

By the end of this article, you will have a solid foundation in NumPy, enabling you to perform sophisticated numerical computations and data manipulations with ease. Let’s embark on this journey to unlock the full potential of NumPy in Python!

Getting Started with NumPy

Getting Started with NumPy

Before diving into the functionalities of NumPy, it is essential to set up your environment and understand some basic concepts. This section will guide you through the installation process and introduce you to the foundational elements of NumPy.

Installing NumPy

The first step in using NumPy is to ensure it is installed in your Python environment. You can install NumPy using pip, the Python package installer. Open your terminal or command prompt and run the following command:

pip install numpy

If you are using a Jupyter notebook, you can install NumPy directly from a cell by running:

!pip install numpy

In a Jupyter notebook, the ! operator allows you to run shell commands directly from within the notebook.

Once installed, you can import NumPy in your Python script or Jupyter notebook:

import numpy as np

The alias np is commonly used and is considered a best practice, making the code more concise and readable.

Basic Concepts and Terminologies

To effectively use NumPy, it’s important to understand some key concepts and terminologies:

  • Array: The central feature of NumPy is the array object. An array is a grid of values, all of the same type, indexed by a tuple of non-negative integers. NumPy arrays are more efficient than Python lists for numerical operations.
  • ndarray: The core object of NumPy is the ndarray, which is a multi-dimensional array of fixed-size items. It is the primary data structure in NumPy.
  • Axis: An axis is a dimension along which operations are performed. For example, a 2D array has two axes: rows (axis 0) and columns (axis 1).
  • Shape: The shape of an array is a tuple of integers representing the size of the array along each dimension. For example, the shape of a 2x3 array is (2, 3).
  • Data Type (dtype): NumPy arrays contain elements of a single type, specified by the dtype object. This ensures that operations on arrays are fast and efficient.

Here’s a simple example to illustrate these concepts:

import numpy as np

array_2d = np.array([[1, 2, 3], [4, 5, 6]])

# Displaying the array
print("Array:\n", array_2d)# Displaying the shape of the array

print("Shape of the array:", array_2d.shape)# Displaying the data type of the array

print("Data type of the array:", array_2d.dtype)# Accessing elements

print("Element at row 0, column 1:", array_2d[0, 1])"""

Output:
Array:
[[1 2 3]
[4 5 6]]Shape of the array: (2, 3)
Data type of the array: int64
Element at row 0, column 1: 2
"""

In this example:

  • We created a 2D array with two rows and three columns.
  • We printed the array, its shape and its data type.
  • We accessed an element in the array using row and column indices.

With the basics covered, you’re now ready to explore more advanced features of NumPy. In the next section, we will delve into creating NumPy arrays using various methods.

Creating NumPy Arrays

Creating NumPy Arrays

NumPy arrays can be created in several ways, providing flexibility and convenience for various use cases. This section will cover the most common methods to create NumPy arrays, including from lists, using built-in functions and more.

Creating Arrays from Lists

The most straightforward way to create a NumPy array is from a Python list using the np.array function. Here’s an example:

# Creating a 1D array from a list
list_1d = [1, 2, 3, 4, 5]
array_1d = np.array(list_1d)
print("1D Array:", array_1d)

# Creating a 2D array (matrix) from a list of lists
list_2d = [[1, 2, 3], [4, 5, 6]]
array_2d = np.array(list_2d)
print("2D Array:\n", array_2d)

# Creating a 3D array from a list of lists of lists
list_3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
array_3d = np.array(list_3d)
print("3D Array:\n", array_3d)

"""
Output:
1D Array: [1 2 3 4 5]
2D Array:
[[1 2 3]
[4 5 6]]
3D Array:
[[[1 2]
[3 4]][[5 6]
[7 8]]]
"""

Using Built-in NumPy Functions

NumPy provides several built-in functions to create arrays quickly and efficiently. Here are some of the most commonly used functions:

  1. np.zeros: Creates an array filled with zeros.
  2. np.ones: Creates an array filled with ones.
  3. np.arange: Creates an array with a range of values.
  4. np.linspace: Creates an array with evenly spaced values over a specified range.
  5. np.random.rand: Creates an array with random values.

Here are examples of each:

# Creating an array of zeros
array_zeros = np.zeros((3, 4))
print("Array of zeros:\n", array_zeros)

# Creating an array of ones
array_ones = np.ones((2, 3))
print("Array of ones:\n", array_ones)

# Creating an array with a range of values
array_arange = np.arange(0, 10, 2)
print("Array with a range of values:", array_arange)

# Creating an array with evenly spaced values
array_linspace = np.linspace(0, 1, 5)
print("Array with evenly spaced values:", array_linspace)

# Creating an array with random values
array_random = np.random.rand(3, 3)
print("Array with random values:\n", array_random)

"""
Output
Array of zeros:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

Array of ones:
[[1. 1. 1.]
[1. 1. 1.]]

Array with a range of values: [0 2 4 6 8]

Array with evenly spaced values: [0. 0.25 0.5 0.75 1. ]

Array with random values:
[[0.39236602 0.10292247 0.26992789]
[0.20464863 0.10948225 0.15399477]
[0.37111632 0.76641267 0.68806162]]

"""

Creating Arrays from Scalars

You can also create arrays by repeating a scalar value. This can be done using the np.full function:

# Creating an array filled with a specific value
array_full = np.full((3, 3), 7)
print("Array filled with 7s:\n", array_full)
"""
Output:
Array filled with 7s:
[[7 7 7]
[7 7 7]
[7 7 7]]
"""

Creating Identity Matrices

Identity matrices are square matrices with ones on the diagonal and zeros elsewhere. These can be created using the np.eye function:

# Creating an identity matrix
identity_matrix = np.eye(4)
print("Identity matrix:\n", identity_matrix)

"""
Output:
Identity matrix:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
"""

These methods cover a wide range of scenarios for creating NumPy arrays. With these tools, you can efficiently initialize arrays for your numerical computations.

Array Operations

Array Operations

NumPy arrays provide a variety of operations that make numerical computations both efficient and straightforward. This section will cover basic arithmetic operations, universal functions (ufuncs) and other useful array operations.

Basic Arithmetic Operations

NumPy allows you to perform element-wise arithmetic operations on arrays. These operations are applied element-by-element and are significantly faster than using Python loops.

Here’s an example demonstrating basic arithmetic operations:

# Creating two arrays
array1 = np.array([1, 2, 3, 4])
array2 = np.array([5, 6, 7, 8])

# Element-wise addition
add_result = array1 + array2
print("Element-wise addition:", add_result)

# Element-wise subtraction
sub_result = array1 - array2
print("Element-wise subtraction:", sub_result)

# Element-wise multiplication
mul_result = array1 * array2
print("Element-wise multiplication:", mul_result)

# Element-wise division
div_result = array1 / array2
print("Element-wise division:", div_result)


"""
Output:
Element-wise addition: [ 6 8 10 12]
Element-wise subtraction: [-4 -4 -4 -4]
Element-wise multiplication: [ 5 12 21 32]
Element-wise division: [0.2 0.33333333 0.42857143 0.5]
"""

Universal Functions (ufuncs)

Universal functions, or ufuncs, are functions that operate element-wise on arrays. NumPy provides a large number of built-in ufuncs for performing mathematical operations.

Here are some examples:

# Creating an array
array = np.array([1, 2, 3, 4])

# Square root of each element
sqrt_result = np.sqrt(array)
print("Square root:", sqrt_result)

# Exponential (e^x) of each element
exp_result = np.exp(array)
print("Exponential:", exp_result)

# Sine of each element
sin_result = np.sin(array)
print("Sine:", sin_result)

# Natural logarithm of each element
log_result = np.log(array)
print("Natural logarithm:", log_result)

"""
Output:
Square root: [1. 1.41421356 1.73205081 2. ]
Exponential: [ 2.71828183 7.3890561 20.08553692 54.59815003]
Sine: [0.84147098 0.90929743 0.14112001 -0.7568025 ]
Natural logarithm: [0. 0.69314718 1.09861229 1.38629436]
"""

Aggregation Functions

NumPy provides several functions for aggregating array values, such as computing the sum, mean, maximum and minimum.

Get CyCoderX’s stories in your inbox

Join Medium for free to get updates from this writer.

Here are some examples:

# Creating an array
array = np.array([1, 2, 3, 4, 5])

# Sum of all elements
sum_result = np.sum(array)
print("Sum:", sum_result)

# Mean (average) of all elements
mean_result = np.mean(array)
print("Mean:", mean_result)

# Maximum value
max_result = np.max(array)
print("Maximum:", max_result)

# Minimum value
min_result = np.min(array)
print("Minimum:", min_result)


"""
Output:
Sum: 15
Mean: 3.0
Maximum: 5
Minimum: 1
"""

Matrix Operations

NumPy also supports matrix operations, which are essential for linear algebra. Here are some basic matrix operations:

# Creating two 2x2 matrices
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# Matrix addition
matrix_add = np.add(matrix1, matrix2)
print("Matrix addition:\n", matrix_add)

# Matrix multiplication
matrix_mul = np.dot(matrix1, matrix2)
print("Matrix multiplication:\n", matrix_mul)

# Transpose of a matrix
matrix_transpose = np.transpose(matrix1)
print("Transpose of the first matrix:\n", matrix_transpose)

"""
Output:

Matrix addition:
[[ 6 8]
[10 12]]

Matrix multiplication:
[[19 22]
[43 50]]

Transpose of the first matrix:
[[1 3]
[2 4]]
"""

These examples illustrate how NumPy simplifies array and matrix operations, making it an indispensable tool for numerical computing in Python.

Indexing and Slicing

Indexing and Slicing

Indexing and slicing are essential for accessing and manipulating elements within a NumPy array. This section will cover the basic and advanced techniques for indexing and slicing NumPy arrays.

Basic Indexing

Basic indexing allows you to access individual elements of an array using their indices. NumPy arrays are zero-indexed, meaning the first element is at index 0.

Here’s an example:

import numpy as np

# Creating a 1D array
array_1d = np.array([10, 20, 30, 40, 50])

# Accessing elements by index
print("First element:", array_1d[0])
print("Third element:", array_1d[2])
print("Last element:", array_1d[-1])


"""
Output:

First element: 10
Third element: 30
Last element: 50
"""

Slicing

Slicing allows you to access a subset of an array. The syntax for slicing is start:stop:step, where start is the starting index, stop is the stopping index (exclusive) and step is the step size.

Here’s an example:

# Creating a 1D array
array_1d = np.array([10, 20, 30, 40, 50])

# Slicing the array
print("Elements from index 1 to 3:", array_1d[1:4])
print("Every second element:", array_1d[::2])
print("Elements from index 2 to the end:", array_1d[2:])

"""
Output:

Elements from index 1 to 3: [20 30 40]
Every second element: [10 30 50]
Elements from index 2 to the end: [30 40 50]
"""

Multi-dimensional Arrays

Indexing and slicing work similarly for multi-dimensional arrays. You can use a comma-separated tuple of indices or slices to access elements.

Here’s an example with a 2D array:

# Creating a 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Accessing elements
print("Element at row 1, column 2:", array_2d[1, 2])
print("First row:", array_2d[0, :])
print("Second column:", array_2d[:, 1])
print("Subarray from rows 0 to 1 and columns 1 to 2:\n", array_2d[0:2, 1:3])

"""
Element at row 1, column 2: 6
First row: [1 2 3]
Second column: [2 5 8]
Subarray from rows 0 to 1 and columns 1 to 2:
[[2 3]
[5 6]]
"""

Boolean Indexing

Boolean indexing allows you to select elements based on conditions. This is particularly useful for filtering data.

Here’s an example:

# Creating an array
array = np.array([10, 20, 30, 40, 50])

# Boolean indexing
bool_idx = array > 30
print("Boolean index array:", bool_idx)

# Using boolean indexing to filter elements
filtered_array = array[bool_idx]
print("Filtered array:", filtered_array)

"""
Output:

Boolean index array: [False False False True True]
Filtered array: [40 50]
"""

Fancy Indexing

Fancy indexing allows you to access multiple array elements at once using a list or array of indices.

Here’s an example:

# Creating an array
array = np.array([10, 20, 30, 40, 50])

# Fancy indexing
indices = [1, 3, 4]
print("Elements at indices 1, 3 and 4:", array[indices])
# Ouput: Elements at indices 1, 3 and 4: [20 40 50]

These techniques provide powerful ways to access and manipulate data within NumPy arrays. Whether you need to extract specific elements, filter data, or create subarrays, NumPy’s indexing and slicing capabilities make it easy and efficient.

Array Manipulation

Array Manipulation

NumPy provides a range of functions for manipulating arrays, allowing you to reshape, concatenate, split and modify arrays in various ways. This section covers some of the most commonly used array manipulation techniques.

Reshaping Arrays

Reshaping allows you to change the shape of an array without altering its data. The reshape function is used to give a new shape to an array.

Here’s an example:

import numpy as np

# Creating a 1D array
array_1d = np.arange(12)
print("Original 1D array:", array_1d)

# Reshaping to a 3x4 2D array
array_2d = array_1d.reshape((3, 4))
print("Reshaped to 3x4 2D array:\n", array_2d)

# Reshaping to a 2x2x3 3D array
array_3d = array_1d.reshape((2, 2, 3))
print("Reshaped to 2x2x3 3D array:\n", array_3d)

"""
Ouput:

Original 1D array: [ 0 1 2 3 4 5 6 7 8 9 10 11]

Reshaped to 3x4 2D array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]

Reshaped to 2x2x3 3D array:
[[ [0 1 2]
[3 4 5]]
[[6 7 8]
[9 10 11]]]
"""

Concatenation and Stacking

Concatenation joins two or more arrays along an existing axis, while stacking joins along a new axis. The np.concatenate and np.stack functions are used for these operations.

Here’s an example of concatenation:

# Creating two 1D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Concatenating along the first axis
concatenated_array = np.concatenate((array1, array2))
print("Concatenated array:", concatenated_array)

# Creating two 2D arrays
array3 = np.array([[1, 2], [3, 4]])
array4 = np.array([[5, 6]])

# Concatenating along the first axis (rows)
concatenated_2d = np.concatenate((array3, array4), axis=0)
print("Concatenated 2D array along rows:\n", concatenated_2d)


"""
Concatenated array: [1 2 3 4 5 6]
Concatenated 2D array along rows:
[[1 2]
[3 4]
[5 6]]
"""

Here’s an example of stacking:

# Creating two 1D arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Stacking along a new axis
stacked_array = np.stack((array1, array2), axis=0)
print("Stacked array along new axis:\n", stacked_array)

"""
Stacked array along new axis:
[[1 2 3]
[4 5 6]]
"""

Splitting Arrays

Splitting allows you to divide an array into multiple sub-arrays. The np.split function is used for this purpose.

Here’s an example:

# Creating a 1D array
array = np.arange(9)

# Splitting the array into 3 sub-arrays
split_array = np.split(array, 3)
print("Split array into 3 sub-arrays:", split_array)

# Creating a 2D array
array2d = np.arange(16).reshape((4, 4))

# Splitting the array into 2 sub-arrays along the rows
split_array2d = np.split(array2d, 2)
print("Split 2D array along rows:\n", split_array2d[0], "\n\n", split_array2d[1])

"""
Output:
Split array into 3 sub-arrays: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
Split 2D array along rows:
[[ 0 1 2 3]
[ 4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]
"""

Modifying Array Shapes

You can also modify the shape of arrays using functions like ravel, flatten and transpose.

Here’s an example:

# Creating a 2D array
array2d = np.array([[1, 2, 3], [4, 5, 6]])

# Flattening the array (convert to 1D)
flattened_array = array2d.flatten()
print("Flattened array:", flattened_array)

# Raveling the array (convert to 1D)
raveled_array = array2d.ravel()
print("Raveled array:", raveled_array)

# Transposing the array (swap rows and columns)
transposed_array = array2d.transpose()
print("Transposed array:\n", transposed_array)


"""
Output:

Flattened array: [1 2 3 4 5 6]
Raveled array: [1 2 3 4 5 6]
Transposed array:
[[1 4]
[2 5]
[3 6]]
"""

These array manipulation techniques provide flexibility and control over your data, allowing you to prepare and transform arrays for various applications.

Keep in mind that the landscape of database technologies is continually evolving, with frequent updates and improvements that could shift these comparisons in the future.
Stay tuned and follow me to keep up-to-date with the latest developments and insights!

Conclusion

In this comprehensive guide, we have explored the vast capabilities of NumPy, a fundamental library for numerical computing in Python.

From basic array creation and manipulation to array operations and manipulation, NumPy offers a robust set of tools for handling and analyzing data.

Here’s a quick recap of what we covered:

  1. Introduction: Understanding the importance of NumPy in numerical computing.
  2. Getting Started with NumPy: Installing NumPy and basic concepts.
  3. Creating NumPy Arrays: Various methods to create arrays from lists, built-in functions and more.
  4. Array Operations: Performing arithmetic operations, using universal functions and matrix operations.
  5. Indexing and Slicing: Accessing and modifying elements within arrays using basic and advanced techniques.
  6. Array Manipulation: Reshaping, concatenating, splitting and modifying arrays.

NumPy is an essential tool for data scientists, engineers and anyone involved in scientific computing. Its efficiency, flexibility and ease of use make it a preferred choice for handling large datasets and performing complex calculations.

As you continue to explore and use NumPy, here are a few tips to keep in mind:

  • Leverage Documentation: NumPy’s official documentation is a valuable resource for understanding functions and finding examples.
  • Practice: The best way to master NumPy is through practice. Work on real-world problems, participate in coding challenges and experiment with different functions and techniques.
  • Community: Engage with the NumPy community through forums, GitHub and other online platforms. Sharing knowledge and seeking help from others can accelerate your learning.
  • Read My articles: Part 2 is comming out soon but you can also explore my profile for more relevant articles.

I hope this guide has provided you with a solid foundation in NumPy and inspired you to delve deeper into the world of numerical computing with Python. Stay tuned for part 2, where I will showcase NumPy’s ability to perform statistical operations, broadcasting and more!

Whether you’re analyzing data, building machine learning models, or conducting scientific research, NumPy is a powerful ally in your coding journey.

If you found this guide helpful, consider exploring more of mycontent on Medium, where we cover a range of topics from data science to software development.

You can find the second part of my article where I delve deeper into Python Arrays for Numerical Computing using Numpy here:

Final Words:

Thank you for taking the time to read my article.

This article was first published on medium by CyCoderX.

Hey There! I’m CyCoderX, a data engineer who loves crafting end-to-end solutions. I write articles about Python, SQL, AI, Data Engineering, lifestyle and more!

Join me as we explore the exciting world of tech, data and beyond!

For similar articles and updates, feel free to explore my Medium profile

If you enjoyed this article, consider following for future updates.

Interested in Python content and tips? Click here to check out my list on Medium.

Interested in more SQL, Databases and Data Engineering content? Click here to find out more!

What did you think about this article? Let me know in the comments below … or above, depending on your device 🙃

--

--

CyCoderX
CyCoderX

Written by CyCoderX

Machine Learning & Data Engineer | Data Science | Python & SQL | AI | Software Developer | Azure & AWS Cloud Specialist | Blogger simplifying Big Data & Trends

Python in Plain English
Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.