Python NumPy

Numpy

Welcome to this beginner-friendly guide to NumPy, created with simplicity in mind. We'll kick things off by answering some common questions and illustrating them with examples to help you understand why NumPy is such a powerful tool. Let’s explore together!
Now, assume that you are playing some sort of computer game where, at each level, you collect coins. You would want to keep track of how much coins you collected in every level. Now, suppose you collect thousands or even millions of coins. How would you keep track of all the numbers?
In real-life applications, scientists, mathematicians, and a data scientist deal with enormous amounts of numbers. Python, the programming language, offers some tools to store numbers, for example lists, but lists are slow and consume a lot of memory. That is where NumPy comes into play! NumPy, in other words numerical Python, is sort of an upgraded list that can work with big numbers quickly and efficiently.

Counting stars in the sky

Think about counting the stars in the night sky. There are so many stars that counting them one by one would take forever! If we could use a tool that quickly counts and organizes the stars, that would make things a lot easier. NumPy is like that tool, but for numbers. It helps us store, manage, and do math with big sets of numbers without slowing down.

Let's discuss the complete tutorial that i have designed just for you!

How do we install NumPy?

Before we start working with NumPy, you should know that it has to be installed on your computer first. That works just the same as downloading an app on your phone; you can do the same with NumPy for your Python environment. Here's how you do this:

  • Using pip (Python's package installer):
1pip install numpy
  • Using Anaconda (a Python distribution that comes with NumPy pre-installed):
    • NumPy is already there! Just open your Python environment and start using it.

Now that we have NumPy installed, let’s start exploring!

NumPy basics: The building blocks

Think of an array as a container that holds a bunch of numbers. It’s like a box where you can store a series of numbers in order, just like how you might line up your toy cars or action figures.

Creating your first NumPy array

Let’s create our first array using NumPy:

python
1import numpy as np  # We use 'np' as a short name for NumPy
2
3# Creating an array of numbers
4my_array = np.array([1, 2, 3, 4, 5])
5print(my_array)

This code creates a simple array with the numbers 1 through 5. Arrays are the most basic yet powerful tool in NumPy.

How do we access elements in an Array?

Remember how you have a favorite toy in your toy box? Similarly, each number in an array has a position or index, starting from 0. Let’s see how we can access these numbers:

python
1# Accessing the first element
2print(my_array[0])  # Output: 1

Intermediate concepts: Getting to know arrays better

How Do We Slice Arrays? Imagine slicing a pizza. Just like you can take a slice of pizza, you can also take a slice of an array. Here’s how:

python
1# Slicing the array from the second element to the fourth
2slice_of_array = my_array[1:4]
3print(slice_of_array)  # Output: [2, 3, 4]

What are Data Types in NumPy?

In NumPy, every array has a data type. Think of it like the material of your toys—some toys are plastic, some are metal. Similarly, numbers in NumPy arrays can be integers, floats (decimals), etc.

python
1# Creating an array with float data type
2float_array = np.array([1.0, 2.0, 3.0], dtype='float')
3print(float_array)

What’s the difference between Copy and View?

Have you ever made a photocopy of a picture? The copy and the original are independent—changing one doesn’t change the other. But if you look at the same picture from a different angle, it's still the same picture. NumPy arrays can also be copied or viewed:

python
1# Copying an array
2copy_array = my_array.copy()
3
4# Viewing an array (this is not a new copy)
5view_array = my_array.view()

Changes to the copied array don’t affect the original, but changes to the view do.

Advanced concepts: Playing with shapes and sizes

How do we change the shape of an Array? Let’s say you have a row of 6 toy cars, and you want to rearrange them into 2 rows of 3 cars each. You can do the same with NumPy arrays by reshaping them:

python
1# Reshaping a 1D array into a 2D array
2reshaped_array = my_array.reshape(2, 3)
3print(reshaped_array)

How do we Join or Split arrays?

Sometimes you want to join two groups of toys or split one group into two. You can do this with arrays too:

  • Joining Arrays:
python
1array1 = np.array([1, 2])
2array2 = np.array([3, 4])
3joined_array = np.concatenate((array1, array2))
4print(joined_array)  # Output: [1 2 3 4]
  • Splitting Arrays:
python
1split_array = np.split(my_array, 3)
2print(split_array)  # Output: [array([1, 2]), array([3, 4]), array([5])]

How do we Sort and Filter arrays?

Just like organizing your toys by color or size, you can sort or filter arrays based on certain criteria:

  • Sorting:
python
1sorted_array = np.sort(my_array)
2print(sorted_array)  # Output: [1 2 3 4 5]
  • Filtering:
python
1filtered_array = my_array[my_array > 3]
2print(filtered_array)  # Output: [4 5]

Exploring NumPy random side

What are random numbers? Have you ever played a board game where you roll a dice? The number you get is random. NumPy can help you generate random numbers, which is useful in games, simulations, and even in science!

python
1# Generating random numbers
2random_numbers = np.random.randint(1, 10, size=5)
3print(random_numbers)

How do we work with distributions?

In nature, some things happen more frequently than others. For example, sunny days might be more common than rainy ones. NumPy can model this with different types of distributions:

  • Normal Distribution: Often used to model things like test scores or heights.
python
1normal_dist = np.random.normal(0, 1, 1000)
  • Binomial Distribution: Used in situations like flipping a coin.
python
1binom_dist = np.random.binomial(n=10, p=0.5, size=1000)

What are universal functions?

Universal functions, or ufuncs, are like magic wands that perform operations on entire arrays in one go. For example, if you want to add 1 to every number in an array, you don’t need to use a loop—NumPy does it for you!

python
1# Adding 1 to each element in the array
2new_array = np.add(my_array, 1)
3print(new_array)  # Output: [2 3 4 5 6]

How do we use arithmetic operations?

NumPy allows you to perform arithmetic operations, like addition, subtraction, multiplication, and division, directly on arrays:

python
1# Multiplying each element by 2
2multiplied_array = np.multiply(my_array, 2)
3print(multiplied_array)  # Output: [2 4 6 8 10]

How do we use trigonometric functions?

If you’ve learned about sine, cosine, and tangent in math class, NumPy has functions for those too!

python
1# Calculating the sine of angles
2angles = np.array([0, np.pi/2, np.pi])
3sine_values = np.sin(angles)
4print(sine_values)

Working with matrix operations

If you like puzzles, matrix operations can be a lot of fun! You can multiply matrices, find their determinants, and do much more with NumPy.

python
1# Matrix multiplication
2matrix1 = np.array([[1, 2], [3, 4]])
3matrix2 = np.array([[5, 6], [7, 8]])
4
5result = np.dot(matrix1, matrix2)
6print(result)

How to use special NumPy functions?

NumPy offers a variety of special functions to make complex tasks easier. Some of these include:

  • Finding the Mean: The average value in an array.
python
1mean_value = np.mean(my_array)
  • Calculating the Standard Deviation: Measures how spread out numbers are.
python
1std_deviation = np.std(my_array)
  • Finding Logarithms: Useful in scientific calculations.
python
1log_values = np.log(my_array)

How to create arrays using special functions

NumPy provides several functions to create arrays without manually specifying the elements. This can be incredibly useful when you need an array with specific properties or patterns.

Using arange() for sequences

The arange() function creates an array with evenly spaced values within a given range. It’s like making a list of numbers without having to write each one out.

python
1# Create an array of numbers from 0 to 9
2sequence_array = np.arange(0, 10)
3print(sequence_array)  # Output: [0 1 2 3 4 5 6 7 8 9]

You can also specify the step size:

python
1# Create an array with a step size of 2
2step_array = np.arange(0, 10, 2)
3print(step_array)  # Output: [0 2 4 6 8]

Using linspace() for Even Spacing

The linspace() function generates an array of evenly spaced numbers over a specified interval. This is like dividing a rope into equal parts.

python
1# Create 5 evenly spaced numbers between 0 and 1
2linspace_array = np.linspace(0, 1, 5)
3print(linspace_array)  # Output: [0.   0.25 0.5  0.75 1.  ]

Creating Arrays of Zeros, Ones, and Identity Matrices

Sometimes, you need an array filled with zeros or ones, or even an identity matrix (a square matrix with ones on the diagonal). NumPy makes this easy:

  • Zeros Array:
python
1zeros_array = np.zeros((3, 3))
2print(zeros_array)

This will create a 3x3 matrix filled with zeros.

  • Ones Array:
python
1ones_array = np.ones((2, 2))
2print(ones_array)

This will create a 2x2 matrix filled with ones.

  • Identity Matrix:
python
1identity_matrix = np.eye(3)
2print(identity_matrix)
  • This will create a 3x3 identity matrix.

Manipulating arrays: How to transpose an array

Transposing an array flips it over its diagonal, turning rows into columns and vice versa. This is useful in many mathematical operations, like solving linear equations.

python
1# Transpose a matrix
2matrix = np.array([[1, 2, 3], [4, 5, 6]])
3transposed_matrix = np.transpose(matrix)
4print(transposed_matrix)

Appending and Concatenating Arrays

You can add new elements to an existing array or combine two arrays together:

  • Appending Elements:
python
1appended_array = np.append(my_array, [6, 7, 8])
2print(appended_array)  # Output: [1 2 3 4 5 6 7 8]

Concatenating Arrays:

python
1concatenated_array = np.concatenate((array1, array2))
2print(concatenated_array)  # Output: [1 2 3 4]

Conclusion: Becoming a NumPy expert

By now, you should have an idea about NumPy-from basic array operations to higher-order matrix manipulations and even generating random numbers. With NumPy, the possibility of doing work in data science, engineering, and beyond becomes so much more immense. The only way to master NumPy is by practicing, so fiddle around with functions and operations. Whether you are doing data analysis, creating simulations, or machine learning, NumPy is one of the tools in Python that would get most of your usage. Keep looking, and you're about to see even more of the things that NumPy can do!

Frequently Asked Questions

Sign-in First to Add Comment

Leave a comment 💬

All Comments

No comments yet.