Sign In
Sign In

How to Read Excel Files in Python using Pandas

How to Read Excel Files in Python using Pandas
Kolawole Mangabo
Technical writer
Python
03.10.2024
Reading time: 8 min

Excel files are commonly used to organize, sort, and analyze data in a tabular format with rows and columns. They are widely applied in industries like data analysis, finance, and reporting.

Using Python, the pandas library allows for efficient manipulation of Excel files, enabling operations like reading and writing data. This article will cover how to use the read_excel function from pandas to read Excel files.

Installing Pandas

To begin, install pandas by running the following command:

pip install pandas

This will install pandas along with the required dependencies in your work environment. Additionally, the openpyxl module is needed for reading .xlsx files.

Why OpenPyXL?

Excel files come in different formats and extensions. To ensure compatibility when working with these files, pandas allows you to specify the engine you want to use. Below is a list of supported engines for reading Excel files:

  • OpenPyXL: Used for reading and writing .xlsx files (Excel 2007+).
  • XlsxWriter: Primarily used for writing .xlsx files.
  • xlrd: Used for reading older .xls files (Excel 97-2003).
  • Pyxlsb: Used for reading .xlsb (binary Excel format) files.

OpenPyXL also supports Excel-specific features, such as formatting and formulas. OpenPyXL is already installed as a dependency of pandas, but you can install it using the following command:

pip install openpyxl

While OpenPyXL can be used on its own to read Excel files, it is also integrated as an engine within pandas for reading and writing .xlsx files.

We will work with an Excel file that you can download here. Download the file and move it into your working environment.

Basic Usage of read_excel Function

The Excel file we are working with has the following structure:

Image1

It also has three worksheets: Orders, Returns, and Users.

To read this file, the read_excel function from pandas will be used.

The read_excel function in pandas is used to import data from Excel files into a pandas DataFrame, a powerful structure for analyzing and manipulating data. This function is highly versatile, allowing users to read data from specific sheets, columns, or ranges.

Here is how to use this function while specifying the engine:

import pandas as pd 

df = pd.read_excel('SuperStoreUS-2015.xlsx')

print(df)

This code imports the pandas library and uses the read_excel function to read the SuperStoreUS-2015.xlsx Excel file into a pandas DataFrame. The print(df) statement outputs the DataFrame contents, displaying the data from the Excel file. Below is the resulting output:

       Row ID Order Priority  Discount  Unit Price  Shipping Cost  ...  Ship Date     Profit Quantity ordered new    Sales Order ID

0      20847           High      0.01        2.84           0.93  ... 2015-01-08     4.5600                    4    13.01    88522

1      20228  Not Specified      0.02      500.98          26.00  ... 2015-06-15  4390.3665                   12  6362.85    90193

2      21776       Critical      0.06        9.48           7.29  ... 2015-02-17   -53.8096                   22   211.15    90192

3      24844         Medium      0.09       78.69          19.99  ... 2015-05-14   803.4705                   16  1164.45    86838

4      24846         Medium      0.08        3.28           2.31  ... 2015-05-13   -24.0300                    7    22.23    86838

The read_excel function is highly flexible and can be adapted to various usage scenarios. Next, we will explore how to use it for reading specific sheets and columns.

Reading Specific Sheets and Columns

Excel files can come with multiple sheets and as many columns as possible. The read_excel function takes the sheet_name argument to tell pandas which sheet to read. By default, read_excel will load all worksheets. Here is how you can use the sheet_name argument:

df = pd.read_excel('SuperStoreUS-2015.xlsx', sheet_name="Returns")

print(df)

This will read the Returns sheet, and here is an example output:

      Order ID    Status

0           65  Returned

1          612  Returned

2          614  Returned

3          678  Returned

4          710  Returned

...        ...       ...

1629    182681  Returned

1630    182683  Returned

1631    182750  Returned

1632    182781  Returned

1633    182906  Returned

[1634 rows x 2 columns]

The sheet_name argument also takes integers that are used in zero-indexed sheet positions. For instance, using pd.read_excel('SuperStoreUS-2015.xlsx', sheet_name=1) will load the Returns sheet as well.

You can also choose to read specific columns from the Excel file. The read_excel function allows for selective column reading using the usecols parameter. It accepts various formats:

  • A string for Excel column letters or ranges (e.g., "A:C").
  • A list of integers for column positions.
  • A list of column names.

Here is an example using column names:

import pandas as pd

df = pd.read_excel('SuperStoreUS-2015.xlsx', usecols=['Row ID', 'Sales'])

print(df)

In this case, the usecols parameter specifies that only columns Row ID and Sales from the Excel file should be imported into the DataFrame. The code below does the same thing, but using Excel column letters:

import pandas as pd

df = pd.read_excel('SuperStoreUS-2015.xlsx', usecols='A,X')

print(df)

Here is the output:

      Row ID    Sales

0      20847    13.01

1      20228  6362.85

2      21776   211.15

3      24844  1164.45

4      24846    22.23

...      ...      ...

1947   19842   207.31

1948   19843   143.12

1949   26208    59.98

1950   24911   135.78

1951   25914   506.50

You can also use range selection to read columns by their position. In the code below, we are reading from Order Priority to Customer ID.

df = pd.read_excel('SuperStoreUS-2015.xlsx', usecols='B:F')

Here is an example output when reading columns B to F:

     Order Priority  Discount  Unit Price  Shipping Cost  Customer ID

0              High      0.01        2.84           0.93            3

1     Not Specified      0.02      500.98          26.00            5

2          Critical      0.06        9.48           7.29           11

3            Medium      0.09       78.69          19.99           14

4            Medium      0.08        3.28           2.31           14

Additionally, you can provide a callable that evaluates column names, reading only those for which the function returns True.

Handling Missing Data in Excel Files

In Excel files, missing data refers to values that are absent, often represented by empty cells. When reading an Excel file into a pandas DataFrame, missing data is automatically identified and handled as NaN (Not a Number), which is pandas placeholder for missing values.

Pandas offers several methods to handle missing data, such as:

  • dropna(): Removes rows or columns with missing values.
  • fillna(): Replaces missing values with a specified value (e.g., 0 or the mean of the column).
  • isna(): Detects missing values and returns a boolean DataFrame.

For example, using fillna on our Excel file will replace all missing values with 0:

df = pd.read_excel('SuperStoreUS-2015.xlsx')

df_cleaned = df.fillna(0)

Handling missing data is essential to ensure accurate analysis and prevent errors or biases in data-driven decisions.

Reading and Analyzing an Excel File in Pandas

Let’s make a pragmatic use of the notion we have learned. In this practical example, we will walk through reading an Excel file, performing some basic analysis, and exporting the manipulated data into various formats. 

Specifically, we’ll calculate the sum, maximum, and minimum values for the Profit column for the year 2015, and export the results to CSV, JSON, and a Python dictionary.

Step 1: Loading the Excel File

The first step is to load the Excel file using the read_excel function from pandas:

import pandas as pd

df = pd.read_excel('SuperStoreUS-2015.xlsx', usecols=['Ship Date', 'Profit'])

print(df.head())

This code reads the SuperStoreUS-2015.xlsx file into a pandas DataFrame and displays the first few rows, including the Ship Date and Profit columns.

Step 2: Calculating Profit for June 2015

Next, we will filter the data to include only records from June 2015 and calculate the total, maximum, and minimum profit for that month. Since the date format in the dataset is MM/DD/YYYY, we will convert the Ship Date column to a datetime format and filter by the specific month:

df['Ship Date'] = pd.to_datetime(df['Ship Date'], format='%m/%d/%Y')

df_june_2015 = df[(df['Ship Date'].dt.year == 2015) & (df['Ship Date'].dt.month == 6)]

# Calculate the sum, max, and min for the Profit column

profit_sum = df_june_2015['Profit'].sum()

profit_max = df_june_2015['Profit'].max()

profit_min = df_june_2015['Profit'].min()

print(f"Total Profit in June 2015: {profit_sum}")

print(f"Maximum Profit in June 2015: {profit_max}")

print(f"Minimum Profit in June 2015: {profit_min}")

The output will be something like:

print(f"Total Profit in June 2015: {round(profit_sum, ndigits=2)}")

print(f"Maximum Profit in June 2015: {round(profit_max, ndigits=2)}")

print(f"Minimum Profit in June 2015: {round(profit_min, ndigits=2)}")

Step 3: Exporting the Manipulated Data

Once the profit for June 2015 has been calculated, we can export the filtered data to different formats, including CSV, JSON, and a Python dictionary.

# Export to CSV

df_june_2015.to_csv('SuperStoreUS_June2015_Profit.csv', index=False)

# Export to JSON

df_june_2015.to_json('SuperStoreUS_June2015_Profit.json', orient='records')

# Convert to Dictionary

data_dict = df_june_2015.to_dict(orient='records')

print(data_dict[:5])

In this step, the data is first exported to a CSV file and then to a JSON file. Finally, the DataFrame is converted into a Python dictionary, with each row represented as a dictionary.

Conclusion

In this article, we have learned how to use the read_excel function from pandas to read and manipulate Excel files. This is a powerful function with the ability to simplify data filtering for a better focus on the rows or columns we want.

Check out our app platform to find Python applications, such as Celery, Django, FastAPI and Flask. 

Python
03.10.2024
Reading time: 8 min

Similar

Python

Functions in Python

Functions in Python are blocks of reusable code that you can access by calling the function name and passing arguments. Using functions in Python significantly simplifies a programmer's work because, instead of writing code repeatedly, one can simply call a function. How to Create a Function in Pyhton Let's start with an example and then move on to the explanation: def multiply(first, second):    return first * second We have just written a function that performs a simple task: it multiplies the values (arguments) passed to it. These values can then be entered after the function name in the program to get the product of the factors. Now, enter the following in IDLE: >>> multiply(7, 8) Arguments can include not only whole numbers but also decimal numbers, for example: >>> multiply(7.4, 8.2)60.68 Now, let's break down the code. Here, we define a Python function using the def keyword and the function name. In parentheses, we specify parameters that will accept various arguments from user input. A colon must follow the closing parenthesis, after which a new line with indentation starts the function body, describing what the function does. If you're writing code in an editor, the indentation will be added automatically. We used the return operator, which explicitly returns arguments. Note that after return, there is an instruction on what the program should do with the arguments. In this case, it multiplies them. Practical Example of Using Python Functions Here, we will demonstrate how Python functions help optimize routine tasks. The following example is simplified but illustrative. By understanding how functions work, you can learn to solve your own tasks, which will become more complex and interesting as you progress in the language. Let's say we opened a bookstore and purchased a cash register, and the cashier had already issued receipts for the first customers. Initially, a receipt might look like this: print("Learn Now, LLC") print("Programming Book", end=" ") print(1, end=" pcs. ") print(50, end=" euro") print("\nAdvanced Programming Book", end=" ") print(1, end=" pcs. ") print(100, end=" euro") print("\nTotal:", 150, end=" euro") print("\nThank you for your purchase!") Output: Learn Now, LLC Programming Book 1 pcs. 50 euro Advanced Programming Book 1 pcs. 100 euro Total: 150 euro Thank you for your purchase! Now, imagine that a whole stack of books has been purchased, and the number of customers is increasing daily. While you manually calculate the total for one customer, others start getting impatient. This is where automation comes in.  Let's say someone buys seven different books, with some books purchased in multiple copies: def check(book_attr): total = 0 print("Learn Now, LLC") for book in book_attr: a = book[0] b = book[1] c = book[2] print(f"{a} ({b} pcs.) - {c} euro") total += b * c print(f"\nTotal: {total} euro") print("Thank you for your purchase!") book_attr = [ ("Programming Book", 2, 50), ("Advanced Programming Book", 2, 100), ("Programming Book 80 lvl", 2, 195), ("Beginner's Guide to Python", 1, 120), ("You Can Become a Programmer", 1, 98), ("Functional Programming in Python", 1, 95), ("Secrets of Clean Code", 1, 80), ] As we can see, new variables appeared, and the purchase list was placed in a separate block. Now, when generating a new receipt, all we need to do for automatic total calculation is enter the book names, quantities, and prices per unit. Once all items are entered, we call our function with the parameter formatted as a tuple above: check(book_attr) This produces the following output: Learn Now, LLC Programming Book (2 pcs.) - 50 euro Advanced Programming Book (2 pcs.) - 100 euro Programming Book 80 lvl (2 pcs.) - 195 euro Beginner's Guide to Python (1 pcs.) - 120 euro You Can Become a Programmer (1 pcs.) - 98 euro Functional Programming in Python (1 pcs.) - 95 euro Secrets of Clean Code (1 pcs.) - 80 euro Total: 1083 euro Thank you for your purchase! That's it! The total amount was calculated automatically. Let’s break down the code: The variable total stores the purchase total and changes as new values are added to the tuple. A for loop is used to define a set of variables that store the following values: a: product name b: quantity c: price per unit Next, we give the print command. The letter f in print statements (which is itself a built-in function, by the way) means that f-strings are used. For now, it's enough to know that they are a convenient formatting method, and the code is self-explanatory. The next line should not be surprising: it calculates the total by multiplying the quantity of each item by its price and adding the result to the running total. Finally, we use another f-string for text formatting, and we have already discussed the tuple block that stores the necessary data for purchase calculations. Features of Functions in Python Key Advantages: No need to repeat specific blocks of code, which can sometimes be quite large. Functions can be called as many times as needed, even consecutively. When divided into multiple functional blocks, large programs become much easier to track. There are almost no downsides to functions in Python, except that they may not always be convenient. In some cases, it is easier to use generators, as certain functions (e.g., filter) may return iterators, requiring additional code to process them. For example, if we enter the following in IDLE: >>> numbers = [2, 4, 6, 8, 10, 12, 14] >>> filter(lambda num: num >= 10, numbers) We get this result: <filter object at 0x00000000030C3220> To correctly display elements that meet the condition, we need to wrap this expression as follows: >>> list(filter(lambda num: num >= 10, numbers))[10, 12, 14] Built-in Functions in Python You have almost certainly used them in your first Python lesson. Here’s an example: print("Hello, World!") The print function is a built-in function, and "Hello, World!" is its argument. Python has hundreds, even thousands, of built-in functions, especially when additional libraries are included. You don't need to know all of them; you can always check the documentation if you encounter an unfamiliar function. However, you will need to learn some common built-in functions, as these core elements are essential for writing any useful program. Here are some commonly used built-in functions: len returns the length (number of elements) of a sequence such as a string, list, tuple, range, or array: flowers = ["bellflower", "cornflower", "buttercup", "forget-me-not", "daisy"]len(flowers) Output: 5 str converts numbers into strings (since Python does not allow direct concatenation of strings and numbers): year = 2008"Euro " + str(year) Output: 'Euro 2008' int converts strings into integers. It also rounds floating-point numbers to the nearest integer, always towards zero: int(554.995) Output: 554 float converts integer values into floating-point numbers, which can be useful for certain calculations: float(55) Output: 55.0 tuple converts lists into tuples: flowers = ["bellflower", "cornflower", "buttercup", "forget-me-not", "daisy"]tuple(flowers) Output: ('bellflower', 'cornflower', 'buttercup', 'forget-me-not', 'daisy') dict allows you to create dictionaries. Here’s an example of creating a dictionary from a list of tuples using dict: clubs = [('Barcelona', 1), ('Juventus', 3), ('Liverpool', 2), ('Real Madrid', 5), ('Bayern München', 4)]dict(clubs) Output: {'Barcelona': 1, 'Juventus': 3, 'Liverpool': 2, 'Real Madrid': 5, 'Bayern München': 4} range creates number sequences, which can be useful for iterating through numeric values: for number in range(0, 30, 3): print(number) Output: 0 3 6 9 12 15 18 21 24 27 The range function takes three parameters: The first two define the range limits. The third (optional) parameter specifies the step. In this case, numbers from 0 to 30 are printed in steps of 3. The upper bound is not included in the output. To include it, the range should be extended slightly: for number in range(0, 31, 3): print(number) Output: 0 3 … 27 30 Using the Result of One Function in Another Python Function Finally, let’s look at another interesting technique. Since functions in Python are objects, they can be passed as arguments to other functions and referenced. def check(company="Learn Now"): """Allows inserting different company names in the receipt""" print(f"{company}, LLC") Let’s enter the name of another company: check("Enlightenment") Output: Enlightenment, LLC Now, let’s pass the created function to the built-in help function to learn what it does: help(check) Output: Help on function check in module __main__: check(company='Learn Now') Allows inserting different company names in the receipt As we can see, it is quite simple. What We Learned In this tutorial, we explored how functions work in Python 3 and learned how to create and use them. We discussed built-in tools and examined an example of passing functions as objects to other functions. By studying functions more deeply, you will appreciate their usefulness even when writing relatively small applications.
02 April 2025 · 8 min to read
Python

Comments in Python 3

Comments in a program are parts of the code that are ignored by the interpreter or compiler. They are used to: Make the code more readable; Explain what the code does and why; Prevent parts of the code from executing during testing/execution; Leave notes about things that need to be done/modified/removed. Overall, comments are meant to make a programmer's life easier—they play no role for the computer. However, in some programming methodologies, such as extreme programming, it is believed that if code needs comments, then the code is poorly written. In this article, you will learn how to write comments in Python 3 and what Docstrings and PEP are. Comments in Python Different programming languages use different syntax for comments. Often, it's a double slash (//). In Python 3, comments in the code start with the # symbol. For example: # The code prints "Hello, World!" to the consoleprint("Hello, World!") You can also place a comment on the same line as the code: print("Hello, World!")  # The code prints "Hello, World!" to the console Comments should be useful to the reader. For example, this comment is not helpful: # This code clearly does somethingprint("Hello, World!") A good comment should explain or describe the code and its purpose. Some developers believe that comments should describe the programmer’s intent. In general, it is best to think of comments as a form of code documentation. If they are not useful, they should be removed. You can also use comments to disable parts of the code to prevent them from executing. This can be useful for testing and debugging. Suppose we need to comment the following code: db_lp = sqlite3.connect('login_password.db') cursor_db = db_lp.cursor() sql_create = '''CREATE TABLE passwords( login TEXT PRIMARY KEY, password TEXT NOT NULL);''' cursor_db.execute(sql_create) db_lp.commit() cursor_db.close() db_lp.close() The goal of commenting is to make this block of code understandable. For example, we can comment it like this: db_lp = sqlite3.connect('login_password.db') # Creating the login_password database cursor_db = db_lp.cursor() # Cursor object for executing SQL queries # SQL query to create the "passwords" table in the database sql_create = '''CREATE TABLE passwords( login TEXT PRIMARY KEY, password TEXT NOT NULL);''' cursor_db.execute(sql_create) # Executing the sql_create query db_lp.commit() # Committing changes # Closing the Cursor and database cursor_db.close() db_lp.close() Manually commenting Python code can be inconvenient. To format a block of code as single-line comments, you can use keyboard shortcuts: PyCharm: Ctrl + / Visual Studio Code: To comment/uncomment a line Ctrl + /, for a block of code Shift + Alt + A Eclipse: To comment/uncomment a line Ctrl + /, for a block of code Ctrl + Shift + / Visual Studio: Ctrl + K then Ctrl + C to comment a block of code, and Ctrl + K then Ctrl + U to uncomment it Docstring in Python A Docstring is a string literal placed immediately after the declaration of a module, function, class, or other structure. It is a convenient way to document code, making it accessible for reference. Docstrings were introduced in Python in 2001 and are described in PEP 257. What is PEP? Python's development follows a structured process involving creating, discussing, selecting, and implementing PEP (Python Enhancement Proposal) documents. PEPs contain proposals for language development, including new features, modifications to existing ones, etc. One of the most well-known and useful PEP documents is PEP 8, which outlines guidelines and conventions for writing Python code. If you plan to write in Python, familiarize yourself with these conventions. Since there are many rules, special tools exist to help enforce them. Some useful tools are listed below. Now, back to Docstring. A Docstring is the first statement in an object's declaration. Here’s an example: def function(x, y, z): """ Docstring of this function """ def inner_function(): """ Docstring of the nested function """ The syntax for a Docstring is three double quotes at the beginning and end. You can also use single quotes or fewer than three quotes, but PEP 257 recommends using three double quotes. You can access an object’s Docstring using the __doc__ method: def subtraction(a, b): """Function subtracts b from a""" return a - b print(subtraction.__doc__) Output: Function subtracts b from a You can also use the __doc__ property to get information about built-in Python methods, such as print: print(print.__doc__) Output: print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. String literals placed anywhere in the Python code can also serve as documentation. The Python bytecode compiler will not recognize them, and they will not be accessible at runtime via __doc__. However, there are two additional types of Docstrings that can be extracted using documentation tools. Additional Docstrings Additional Docstrings are string literals ignored by the Python compiler but recognized by Docutils tools. They are placed immediately after a Docstring. Example: def function(arg): """This is the Docstring of this function. It will be available via __doc__.""" """ This is an additional Docstring. It will be ignored by the compiler but recognized by Docutils. """ pass Attribute Docstrings Attribute Docstrings are string literals placed immediately after a simple assignment at the module, class, or __init__ method level. Example: def f(x): """This is the Docstring of this function. It will be available via __doc__""" return x**2 f.a = 1 """ This is an Attribute Docstring for the attribute f.a, it will be ignored by the compiler but recognized by Docutils. """ Here are the main PEP 257 guidelines for using docstrings: Leave a blank line after all Docstrings. The script's Docstring should serve as a "usage message," potentially displayed to the user if incorrect arguments are provided. It should describe functionality, parameter syntax, environment variables, and files used. The module's Docstring should list important objects with a one-line explanation for each. The function/method Docstring should describe behavior, arguments, return values, possible exceptions, and constraints. The class Docstring should include methods, instance variables, and describe the class behavior. The constructor (__init__) should have its own separate Docstring. If a class is a subclass and mostly inherits behavior from a parent class, its documentation should mention this and describe any differences. Useful Tools Here are some tools to help with PEP 8 and comments in Python 3: pycodestyle — Checks if your code follows PEP 8. Black — Formats code according to PEP 8 (mostly). Doxygen, PyDoc, pdoc — Automatically generate documentation from Docstrings.
02 April 2025 · 6 min to read
Python

How to Install and Set Up PyTorch

PyTorch is a free, open-source deep learning library. With its help, a computer can detect objects, classify images, generate text, and perform other complex tasks. PyTorch is also a rich tool ecosystem that supports and accelerates AI development and research. In this article, we will cover only the basics: we will learn how to install PyTorch and verify that it works. To work with PyTorch, you will need: At least 1 GB of RAM. Installed Python 3 and pip.  A configured local development environment. Deep knowledge of machine learning is not required for this tutorial. It is assumed that you are familiar with basic Python terms and concepts. Installing PyTorch We will be working in a Windows environment but using the command line. This makes the tutorial almost universal—you can use the same commands on Linux and macOS. First, create a workspace where you will work with Torch Python. Navigate to the directory where you want to place the new folder and create it: mkdir pytorch Inside the pytorch directory, create a new virtual environment. This is necessary to isolate projects and, if needed, use different library versions. python3 -m venv virtualpytorch To activate the virtual environment, first go to the newly created directory: cd virtualpytorch Inside, there is a scripts folder (on Windows) or bin (on other OS). Navigate to it: cd scripts Activate the virtual environment using a bat file by running the following command in the terminal: activate.bat The workspace is now ready. The next step is to install the PyTorch library. The easiest way to find the installation command is to check the official website. There is a convenient form where you select the required parameters. As an example, install the stable version for Windows using CPU via pip. Select these parameters in the form, and you will get the necessary command: pip3 install torch torchvision torchaudio Copy and execute the pip install torch command in the Windows command line. You are also installing two sub-libraries: torchvision – contains popular datasets, model architectures, and image transformations for computer vision. torchaudio – a library for processing audio and signals using PyTorch, providing input/output functions, signal processing, datasets, model implementations, and application components. This is the standard setup often used when first exploring the library. The method described above is not the only way to install PyTorch. If Anaconda is installed on Windows, you can use its graphical interface. If your computer has NVIDIA GPUs, you can select the CUDA version instead of CPU. In that case, the installation command will be different. All possible local installation methods are listed in the official documentation. You can also find commands for installing older versions of the library there. To install them, just select the required version and install it the same way as the current package builds. You don't need to write a script to check if the library is working. The Python interpreter has enough capabilities to perform basic operations. If you have successfully installed PyTorch in the previous steps, then launching the Python interpreter won’t be an issue. Run the following command in the command line: python Then enter the following code: import torch x = torch.rand(5, 3) print(x) You should see an output similar to this: tensor([[0.0925, 0.3696, 0.4949], [0.0240, 0.2642, 0.1545], [0.7274, 0.4975, 0.0753], [0.4438, 0.9685, 0.5022], [0.4757, 0.6715, 0.4298]]) Now, you can move on to solving more complex tasks. PyTorch Usage Example To make learning basic concepts more engaging, let’s do it in practice. For example, let’s create a neural network using PyTorch that can recognize the digit shown in an image. Prerequisites To create a neural network, we need to import eight modules: import torch import torchvision import torch.nn.functional as F import matplotlib.pyplot as plt import torch.nn as nn import torch.optim as optim from torchvision import transforms, datasets All of these are standard PyTorch libraries plus Matplotlib. They handle image processing, optimization, neural network construction, and graph visualization. Loading and Transforming Data We will train the neural network on the MNIST dataset, which contains 70,000 images of handwritten digits. 60,000 images will be used for training. 10,000 images will be used for testing. Each image is 28 × 28 pixels. Each image has a label representing the digit (e.g., 1, 2, 5, etc.). train = datasets.MNIST("", train=True, download=True, transform=transforms.Compose([transforms.ToTensor()])) test = datasets.MNIST("", train=False, download=True, transform=transforms.Compose([transforms.ToTensor()])) trainset = torch.utils.data.DataLoader(train, batch_size=15, shuffle=True) testset = torch.utils.data.DataLoader(test, batch_size=15, shuffle=True) First, we divide the data into training and testing sets by setting train=True/False. The test set must contain data that the machine has not seen before. Otherwise, the neural network’s performance would be biased. Setting shuffle=True helps reduce bias and overfitting. Imagine that the dataset contains many consecutive "1"s. If the machine gets too good at recognizing only the digit 1, it might struggle to recognize other numbers. Shuffling the data prevents the model from overfitting specific patterns and ensures a more generalized learning process. Definition and Initialization of the Neural Network The next step is defining the neural network: class NeuralNetwork(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 86) self.fc2 = nn.Linear(86, 86) self.fc3 = nn.Linear(86, 86) self.fc4 = nn.Linear(86, 10) def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) return F.log_softmax(x, dim=1) model = NeuralNetwork() The neural network consists of four layers: one input layer, two hidden layers, and one output layer. The Linear type represents a simple neural network. For each layer, it is necessary to specify the number of inputs and outputs. The output number of one layer becomes the input for the next layer. The input layer has 784 nodes. This is the result of multiplying 28 × 28 (the image size in pixels). The first hidden layer has 86 output nodes, so the input to the next layer must be 86 as well.The same logic applies further. 86 is an arbitrary number—you can use a different value. The output layer contains 10 nodes because the images represent digits from 0 to 9. Each time data passes through a layer, it is processed by an activation function. There are several activation functions. In this example, we use ReLU (Rectified Linear Unit). This function returns 0 if the value is negative or the value itself if it is positive. The softmax function is used at the output layer to normalize values. For example, it might return an 80% probability that the digit in the image is 1, or a 30% probability that the digit is 5, and so on. The highest probability is selected as the final prediction. Training The next step is training. optimizer = optim.Adam(model.parameters(), lr=0.001) EPOCHS = 3 for epoch in range(EPOCHS): for data in trainset: X, y = data model.zero_grad() output = model(X.view(-1, 28 * 28)) loss = F.nll_loss(output, y) loss.backward() optimizer.step() print(loss) The optimizer calculates the difference (loss) between the actual data and the prediction, adjusts the weights, recalculates the loss, and continues the cycle until the loss is minimized. Training Verification Here, we compare the actual values with the predictions made by the model. For this tutorial, the accuracy is high because the neural network effectively recognizes each digit. correct = 0 total = 0 with torch.no_grad(): for data in testset: data_input, target = data output = model(data_input.view(-1, 784)) for idx, i in enumerate(output): if torch.argmax(i) == target[idx]: correct += 1 total += 1 print('Accuracy: %d %%' % (100 * correct / total)) To verify that the neural network works, pass it an image of a digit from the test set: plt.imshow(X[1].view(28,28)) plt.show() print(torch.argmax(model(X[1].view(-1, 784))[0])) The output should display the digit shown in the provided image. Final Script Here’s the full script you can run to see how the neural network works: import torch import torchvision import torch.nn.functional as F import matplotlib.pyplot as plt import torch.nn as nn import torch.optim as optim from torchvision import transforms, datasets train = datasets.MNIST("", train=True, download=True, transform = transforms.Compose([transforms.ToTensor()])) test = datasets.MNIST("", train=False, download=True, transform = transforms.Compose([transforms.ToTensor()])) trainset = torch.utils.data.DataLoader(train, batch_size=15, shuffle=True) testset = torch.utils.data.DataLoader(test, batch_size=15, shuffle=True) class NeuralNetwork(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 86) self.fc2 = nn.Linear(86, 86) self.fc3 = nn.Linear(86, 86) self.fc4 = nn.Linear(86, 10) def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) return F.log_softmax(x, dim=1) model = NeuralNetwork() optimizer = optim.Adam(model.parameters(), lr=0.001) EPOCHS = 3 for epoch in range(EPOCHS): for data in trainset: X, y = data model.zero_grad() output = model(X.view(-1, 28 * 28)) loss = F.nll_loss(output, y) loss.backward() optimizer.step() print(loss) correct = 0 total = 0 with torch.no_grad(): for data in testset: data_input, target = data output = model(data_input.view(-1, 784)) for idx, i in enumerate(output): if torch.argmax(i) == target[idx]: correct += 1 total += 1 print('Accuracy: %d %%' % (100 * correct / total)) plt.imshow(X[1].view(28,28)) plt.show() print(torch.argmax(model(X[1].view(-1, 784))[0])) Each time we run the network, it will take a random image from the test set and analyze the digit depicted on it. After the process is completed, it will display the recognition accuracy in percentage, the image itself, and the digit recognized by the neural network. This is how it looks: Conclusion PyTorch is a powerful open-source machine learning platform that accelerates the transition from research prototypes to production deployments. With it, you can solve various tasks in the fields of artificial intelligence and neural networks. You don’t need deep knowledge of machine learning to begin working with PyTorch. It is enough to know the basic concepts to repeat and even modify popular procedures like image recognition to suit your needs. A big advantage of PyTorch is the large user community that writes tutorials and shares examples of using the library. Object recognition in images is one of the simplest and most popular tasks in PyTorch for beginners. However, the capabilities of the library are not limited to this. To create powerful neural networks, you need a lot of training data. These can be stored, for example, in an object-based S3 storage such as Hostman, with instant data access via API or web interface. This is an excellent solution for storing large volumes of information.
01 April 2025 · 10 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support