Basics of the NumPy library: arrays, matrices and mathematical operations

онлайн тренажер по питону
Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to the NumPy Library

The NumPy library is a fundamental building block for scientific computing in Python. With NumPy, Python developers can efficiently work with multidimensional arrays and matrices and perform high-performance mathematical operations.

NumPy is an integral part of the ecosystem for data analysis, machine learning, and engineering calculations. This library is actively used by popular frameworks such as Pandas, Scikit-learn, TensorFlow, and many other scientific programming tools.

This material provides a complete guide to NumPy arrays, which will help you master working with arrays and matrices. You will learn how to perform arithmetic and logical operations, as well as efficiently combine and modify data structures.

Installation and Initial Setup of the Library

The NumPy Installation Process

To install the NumPy Python library, use the pip package manager:

pip install numpy

Verifying Correct Installation

After installation, it is recommended to check the library version:

import numpy as np
print(np.__version__)

This command will output the current version of the installed library, confirming successful installation.

Core NumPy Objects: Working with Arrays

What is ndarray?

The main object of the library is the array (ndarray). This is an improved version of standard Python lists, specifically optimized for fast mathematical operations. NumPy arrays are stored more compactly in memory and processed much faster than regular lists.

Creating One-Dimensional Arrays

The simplest way to create an array:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)  # [1 2 3 4 5]

Working with Multidimensional Arrays

To create a two-dimensional array (matrix):

matrix = np.array([[1, 2], [3, 4]])
print(matrix)

Result:

[[1 2]
 [3 4]]

Basic Properties of NumPy Arrays

Key Attributes of Arrays

Each NumPy array has a set of important properties:

  • ndim - the number of array dimensions
  • shape - the size of the array as a tuple
  • size - the total number of elements in the array
  • dtype - the data type of the elements in the array

Practical Use of Properties

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim)   # 2 (two-dimensional array)
print(arr.shape)  # (2, 3) (2 rows, 3 columns)
print(arr.size)   # 6 (6 elements in total)
print(arr.dtype)  # int64 (data type)

Understanding these properties is critical for effectively working with NumPy arrays of various dimensions.

Special Functions for Creating Arrays

Arrays with Predefined Values

NumPy provides convenient functions for creating special types of arrays:

  • np.zeros() - create an array of zeros
  • np.ones() - create an array of ones
  • np.eye() - create an identity matrix
  • np.full() - create an array with a specified value
zeros_array = np.zeros((2, 3))     # 2x3 array of zeros
ones_array = np.ones((3, 3))       # 3x3 array of ones
identity = np.eye(3)               # 3x3 identity matrix

Functions for Generating Sequences

  • np.arange() - create an arithmetic progression
  • np.linspace() - create evenly spaced values
range_array = np.arange(0, 10, 2)      # [0 2 4 6 8]
linear_space = np.linspace(0, 1, 5)    # [0.   0.25 0.5  0.75 1.  ]

Mathematical Operations in NumPy

Elementary Arithmetic Operations

NumPy allows you to perform operations directly on arrays without using loops. This is called vectorization of operations:

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

print(a + b)    # [5 7 9]  - element-wise addition
print(a * b)    # [4 10 18] - element-wise multiplication
print(a ** 2)   # [1 4 9]  - exponentiation
print(a / b)    # [0.25 0.4 0.5] - division

Built-in Mathematical Functions

NumPy contains an extensive library of mathematical functions:

  • Statistical functions: np.sum(), np.mean(), np.median(), np.std()
  • Search functions: np.max(), np.min(), np.argmax(), np.argmin()
  • Trigonometric functions: np.sin(), np.cos(), np.tan()
  • Exponential functions: np.exp(), np.log(), np.sqrt()
arr = np.array([1, 2, 3, 4, 5])
print(np.sum(arr))      # 15 - sum of elements
print(np.mean(arr))     # 3.0 - average value
print(np.max(arr))      # 5 - maximum element
print(np.sin(arr))      # sine of each element
print(np.sqrt(arr))     # square root of each element

Operations with Array Axes

Many functions support the axis parameter to perform operations along a specific axis:

matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(np.sum(matrix, axis=0))  # [5 7 9] - sum by columns
print(np.sum(matrix, axis=1))  # [6 15] - sum by rows

Working with Matrices in NumPy

Matrix Objects

Although you can use regular arrays for matrix operations in NumPy, the library provides a special matrix object for convenience:

mat = np.matrix([[1, 2], [3, 4]])
print(mat.T)  # Transpose of the matrix
print(mat.I)  # Inverse matrix

Matrix Operations with Arrays

To perform matrix operations with regular arrays, use special functions:

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

# Matrix multiplication
result = np.dot(a, b)
# or using the @ operator
result = a @ b

# Transposition
transposed = np.transpose(a)
# or
transposed = a.T

Determinant and Eigenvalues

matrix = np.array([[1, 2], [3, 4]])
determinant = np.linalg.det(matrix)      # Determinant
eigenvalues = np.linalg.eigvals(matrix)  # Eigenvalues

NumPy Array Concatenation

Horizontal Concatenation of Arrays

To concatenate arrays horizontally, use the hstack() function:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
horizontal = np.hstack((a, b))  # [1 2 3 4 5 6]

Vertical Concatenation of Arrays

The vstack() function allows you to concatenate arrays vertically:

vertical = np.vstack((a, b))

Result:

[[1 2 3]
 [4 5 6]]

Universal concatenate() Function

The concatenate() function provides more flexible options for NumPy array concatenation:

# Concatenation along the first axis (rows)
result1 = np.concatenate((a, b), axis=0)

# For two-dimensional arrays
matrix_a = np.array([[1, 2], [3, 4]])
matrix_b = np.array([[5, 6], [7, 8]])

# Concatenation by columns
horizontal_concat = np.concatenate((matrix_a, matrix_b), axis=1)

# Concatenation by rows
vertical_concat = np.concatenate((matrix_a, matrix_b), axis=0)

Additional Concatenation Functions

  • np.append() - add elements to the end of the array
  • np.insert() - insert elements at a specific position
  • np.stack() - concatenate arrays along a new axis

Indexing and Slicing Arrays

Basic Indexing

NumPy arrays support various indexing methods:

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

print(arr[0, 1])    # 2 - element of the first row, second column
print(arr[:, 1])    # [2 5] - all elements of the second column
print(arr[1, :])    # [4 5 6] - all elements of the second row

Advanced Indexing

NumPy supports indexing with arrays and logical conditions:

arr = np.array([10, 20, 30, 40, 50])

# Indexing with an array
indices = np.array([0, 2, 4])
print(arr[indices])  # [10 30 50]

# Logical indexing
mask = arr > 25
print(arr[mask])     # [30 40 50]

Slices of Multidimensional Arrays

matrix = np.array([[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 10, 11, 12]])

print(matrix[:2, 1:3])  # First two rows, columns from 1 to 2
print(matrix[::2, ::2]) # Every second row and column

Logical Operations and Filtering

Applying Logical Conditions

NumPy provides powerful tools for filtering data:

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

# Filtering by condition
filtered = arr[arr > 3]        # [4 5 6]
even_numbers = arr[arr % 2 == 0]  # [2 4 6]

Functions for Working with Conditions

  • np.where() - find the indices of elements that satisfy the condition
  • np.any() - check if there is at least one True
  • np.all() - check if all elements are True
arr = np.array([1, 2, 3, 4, 5])

# Indices of even numbers
even_indices = np.where(arr % 2 == 0)
print(even_indices)  # (array([1, 3]),)

# Conditional replacement of values
result = np.where(arr > 3, arr, 0)  # [0 0 0 4 5]

Combining Logical Conditions

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# Using logical operators & (and), | (or), ~ (not)
condition = (arr > 3) & (arr < 8)
filtered = arr[condition]  # [4 5 6 7]

# Multiple conditions
complex_condition = (arr % 2 == 0) | (arr > 8)
result = arr[complex_condition]  # [2 4 6 8 9 10]

Examples of NumPy Applications in Real Projects

Data Analysis and Statistics

NumPy is widely used for fast processing of large data arrays:

  • Loading and processing CSV files with millions of records
  • Calculating statistical indicators for time series
  • Normalizing and standardizing data before analysis
  • Grouping and aggregating information by various criteria

Machine Learning and Artificial Intelligence

In the field of machine learning, NumPy plays a key role:

  • Vectorizing input data for training models
  • Implementing optimization algorithms using gradient descent
  • Processing images and converting them into numerical arrays
  • Calculating model quality metrics and cross-validation

Engineering Calculations and Modeling

NumPy is indispensable in technical calculations:

  • Solving systems of linear equations of large dimensions
  • Numerical integration and differentiation
  • Modeling physical processes and simulations
  • Signal processing and digital filtering

Graphics and Data Visualization

The library serves as the basis for many graphics applications:

  • Fast conversion of images into numerical arrays
  • Applying filters and effects to images
  • Generating data for plotting graphs and charts
  • Creating animations based on numerical data

Useful Functions for Working with Arrays

Functions for Analyzing Unique Values

  • np.unique() - getting unique elements of an array with the possibility of counting frequency
  • np.bincount() - counting the number of occurrences of each value
  • np.histogram() - building a histogram of the distribution of values
arr = np.array([1, 2, 2, 3, 3, 3, 4])
unique_values = np.unique(arr, return_counts=True)
print(unique_values)  # (array([1, 2, 3, 4]), array([1, 2, 3, 1]))

Sorting and Ordering Functions

  • np.sort() - sorting an array in ascending or descending order
  • np.argsort() - getting indices for sorting
  • np.lexsort() - complex sorting by multiple keys
arr = np.array([3, 1, 4, 1, 5])
sorted_arr = np.sort(arr)           # [1 1 3 4 5]
sort_indices = np.argsort(arr)      # [1 3 0 2 4]

Functions for Reshaping Arrays

  • np.reshape() - changing the dimension of an array
  • np.flatten() - converting to a one-dimensional array
  • np.resize() - changing the size with possible repetition of elements
  • np.ravel() - "unfolding" a multidimensional array
matrix = np.array([[1, 2, 3], [4, 5, 6]])
reshaped = matrix.reshape(3, 2)     # Reshaping to 3x2
flattened = matrix.flatten()        # [1 2 3 4 5 6]

Conclusion

The NumPy library is a fundamental tool for working with data and computations in Python. Mastering the basics of NumPy opens up wide opportunities for solving problems in various fields of science and technology.

Key benefits of using NumPy:

  • Efficient processing of arrays and matrices of any dimension
  • Performing complex mathematical operations in fractions of a second
  • Working with large amounts of data without loss of performance
  • Integration with other libraries in the Python ecosystem
  • Support for various data types and memory optimization

NumPy arrays provide a significant advantage in speed compared to standard Python lists due to implementation in C and vectorization of operations. This makes the library indispensable for scientific computing, data analysis, and machine learning.

To deepen knowledge and find solutions to non-standard problems, it is recommended to regularly refer to the official documentation and study examples of practical application in real projects.

Frequently Asked Questions

What is the NumPy library?

NumPy is a Python library for scientific computing that provides support for multidimensional arrays and a wide range of mathematical functions for processing them.

How to perform NumPy array concatenation in various ways?

To concatenate arrays, use the functions np.hstack() for horizontal concatenation, np.vstack() for vertical concatenation, and np.concatenate() for universal concatenation with axis specification.

Where to find detailed documentation on NumPy in Russian?

It is recommended to study the official NumPy documentation, as well as detailed guides and tutorials available in the Russian-speaking Python developer community.

What is the advantage of NumPy arrays over standard Python lists?

NumPy arrays work many times faster than standard lists due to implementation in C, optimization of memory usage, and support for vectorized operations on all elements simultaneously.

What are the main areas of application of NumPy?

NumPy is widely used in data analysis, machine learning, scientific computing, image processing, engineering calculations, and wherever efficient work with numerical arrays is required.

News