Home >Backend Development >Python Tutorial >Learn how to use the numpy library for data analysis and scientific computing

Learn how to use the numpy library for data analysis and scientific computing

PHPz
PHPzOriginal
2024-01-19 08:05:11961browse

Learn how to use the numpy library for data analysis and scientific computing

With the advent of the information age, data analysis and scientific computing have become an important part of more and more fields. In this process, the use of computers for data processing and analysis has become an indispensable tool. In Python, the numpy library is a very important tool, which allows us to process and analyze data more efficiently and get results faster. This article will introduce the common functions and usage of numpy, and give some specific code examples to help you learn in depth.

  1. Installation and calling of numpy library

Before we start, we need to install the numpy library first. Just enter the following command on the command line:

!pip install numpy

After the installation is complete, we need to call the numpy library in the program. You can use the following statement:

import numpy as np

Here, we use the import command to introduce the numpy library into the program, and use the alias np to replace the name of the library. This alias can be changed according to personal preference.

  1. Commonly used functions of the numpy library

The numpy library is a library specifically used for scientific computing and has the following characteristics:

  • High High-performance multi-dimensional array calculation
  • Perform fast mathematical operations and logical operations on arrays
  • A large number of mathematical function libraries and matrix calculation libraries
  • Tools for reading and writing disk files

Let’s introduce some common functions of the numpy library.

2.1 Create numpy array

One of the most important functions of numpy is to create arrays. The easiest way to create an array is to use the np.array() function. For example:

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

This line of code creates a one-dimensional array containing the values ​​​​[1, 2, 3].

We can also create multi-dimensional arrays, for example:

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

This sentence creates a one-dimensional array containing two [1,2,3] and [4,5,6] is a two-dimensional array.

You can also use some preset functions to create arrays, such as:

zeros_arr = np.zeros((3, 2))   # 创建一个二维数组,每个元素为0
ones_arr = np.ones(4)          # 创建一个一维数组,每个元素为1
rand_arr = np.random.rand(3,4) # 创建一个3行4列的随机数组

2.2 Array indexing and slicing

Through indexing and slicing, we can access and access numpy arrays Modify operations. For one-dimensional arrays, we can use the following methods to access:

arr = np.array([1, 2, 3, 4, 5])
print(arr[0])    # 输出第一个元素
print(arr[-1])   # 输出最后一个元素
print(arr[1:3])  # 输出索引为1到2的元素
print(arr[:3])   # 输出前三个元素
print(arr[3:])   # 输出后三个元素

For multi-dimensional arrays, we can use the following methods to access:

arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr2d[0][0])   # 输出第一行第一个元素
print(arr2d[1, :])   # 输出第二行所有元素
print(arr2d[:, 1])   # 输出第二列所有元素

2.3 Array operations

numpy provides A variety of array operation methods. Specifically, these operations include addition, subtraction, multiplication, division, average, variance, standard deviation, dot product, and more.

arr = np.array([1, 2, 3])
print(arr + 1)   # 对数组每个元素加1
print(arr * 2)   # 对数组每个元素乘2
print(arr / 3)   # 对数组每个元素除以3
print(np.mean(arr))    # 求数组平均数
print(np.var(arr))     # 求数组方差
print(np.std(arr))     # 求数组标准差

2.4 Array shape transformation

Sometimes, we need to transform the shape of numpy array. Numpy provides many practical tools for this purpose.

arr = np.array([1, 2, 3, 4, 5, 6])
print(arr.reshape((2, 3)))    # 将数组改变成两行三列的形状
print(arr.reshape((-1, 2)))   # 将数组改变成两列的形状
print(arr.reshape((3, -1)))   # 将数组改变成三行的形状

2.5 Matrix calculation

numpy also provides a large number of matrix calculation tools, such as dot products and transformations.

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
print(np.dot(arr1, arr2))    # 计算两个矩阵的点积
print(arr1.T)               # 将矩阵进行转置
  1. Example code

Next, we give some specific code examples to help you better understand how to use numpy.

3.1 Create a random array and calculate the mean

arr = np.random.rand(5, 3)    # 创建一个5行3列的随机数组
print(arr)
print(np.mean(arr))           # 计算数组元素的平均值

Output:

[[0.36112019 0.66281023 0.76194693]
 [0.13728812 0.2015571  0.2047288 ]
 [0.90020599 0.46448655 0.31758295]
 [0.9980158  0.56503496 0.98733627]
 [0.84116752 0.68022348 0.49029864]]
0.5444867833241556

3.2 Calculate the standard deviation and variance of the array

arr = np.array([1, 2, 3, 4, 5])
print(np.std(arr))    # 计算数组的标准差
print(np.var(arr))    # 计算数组的方差

Output:

1.4142135623730951
2.0

3.3 Convert the array into a matrix and calculate the matrix dot product

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
mat1 = np.mat(arr1)    # 将数组转换成矩阵
mat2 = np.mat(arr2)    
print(mat1 * mat2)     # 计算矩阵点积

Output:

[[19 22]
 [43 50]]

This article introduces the common functions and usage of the numpy library, and gives some specific Code examples to help you better understand the use of numpy. As the importance of data analysis and scientific computing continues to increase in daily life, it has also promoted the widespread use of the numpy library. I hope this article can help everyone better master the use of numpy, so as to process and analyze data more efficiently.

The above is the detailed content of Learn how to use the numpy library for data analysis and scientific computing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn