Django Tutorial for Beginners
■Episode 11: How to Use NumPy and Its Basic Concepts
(Last updated: 2023.06.03)
Image of Django framework
This article takes about 5 minutes to read!
(Rotate your phone sideways if the image appears small)
"If you're using Python, use NumPy!"
Django is a framework written in Python. Therefore, using it with Python’s powerful libraries makes it an even stronger tool. This time, we’ll focus on one such powerful library — NumPy.
NumPy is particularly suited for handling numerical data in arrays and matrices, and is known for its fast numerical operations and statistical analysis features. Because it excels in handling multi-dimensional arrays, it is widely used in data science, machine learning, image processing, and scientific computing. On the web, it’s easy to imagine how image processing and statistical calculations can have many applications.
Statistical analysis with Python
Image of statistical analysis using Python
Over the next few articles, we’ll explore how to use NumPy and why it’s so useful, starting with the basics.
[Table of Contents]
Why NumPy Is Essential for Python Programmers
Installing NumPy
Basic Concepts of NumPy
Summary
1. Why NumPy Is Essential for Python Programmers
Python is widely used due to its flexibility and readability. However, its execution speed is relatively slow, which makes it less ideal for processing large amounts of numerical data. This is where NumPy comes in. Written in C and FORTRAN, NumPy can efficiently process large-scale numerical data. Beyond calculations, it also supports data reshaping and extraction — making it a comprehensive tool for data processing.
C language
Image representing the C programming language
Moreover, NumPy serves as the foundation for many other data analysis libraries such as Pandas, SciPy, and Matplotlib. Its compatibility with these tools provides a consistent and efficient data handling experience across the Python ecosystem.
Starting now, let’s dive into NumPy from the basics to advanced features with concrete examples.
2. Installing NumPy
Before installing NumPy, let’s start by installing Python itself. Python can be installed on Windows, macOS, and Linux. Download it from the official website and install it.
Once Python is installed, we recommend setting up a virtual environment rather than installing libraries globally. Virtual environments help isolate project dependencies and ensure consistent environments. Python includes a built-in module called `venv` for this purpose. You can create a virtual environment by typing `python3 -m venv myenv` in the terminal (or Command Prompt on Windows). The `3` in `python3` may not be necessary depending on your system. (If the built-in Python on Windows doesn't work well with `venv`, try installing Python from the official website.) You can name the environment whatever you like — here we use `myenv`. To activate the virtual environment, use the following commands: The first line is for Windows, and the second is for Linux/macOS.
Activate virtual environment
Activating the virtual environment
Next, install NumPy in the virtual environment using pip. It’s a simple one-line command as shown below.
Command to install NumPy
Command to install NumPy
Once installed, include `import numpy as np` at the top of your Python script to use NumPy. That’s how to set up the environment for NumPy.
3. Basic Concepts of NumPy
Before using NumPy, there are three basic concepts you should understand: arrays, matrices, and vectors. These are all related to the core data structure in NumPy — the NumPy array.
A NumPy array is a multidimensional, linear structure that stores data of the same type (mostly numerical). It is similar to Python lists but is superior in terms of speed and memory efficiency. NumPy arrays can be 0-dimensional (scalar), 1D (vector), 2D (matrix), or even higher dimensions.
Various NumPy arrays
Various types of NumPy arrays
A matrix is a 2D NumPy array used for mathematical operations like multiplication, inversion, or computing the rank. It has rows and columns, making it suitable for storing 2D data.
A vector is a 1D NumPy array used for mathematical operations such as addition, scalar multiplication, or dot product. Understanding these core concepts will help you work with data and perform calculations more easily using NumPy. In the next section, we’ll look at how to use these concepts in practice.
4. Summary
In this article, we covered how to install Python, create a virtual environment using venv, and install NumPy. We also explained the basic concepts and data structures in NumPy — arrays, matrices, and vectors. We haven’t used them yet, but make sure you can run NumPy on your PC before moving forward.
▼References
Creating and Activating a Virtual Environment with venv on Windows - Zenn
Basics of NumPy Arrays - Machine Learning Meets Python