Home > Backend Development > Python Tutorial > From entry to proficiency: Master the basic operations and common functions of the numpy library

From entry to proficiency: Master the basic operations and common functions of the numpy library

王林
Release: 2024-01-19 10:33:17
Original
1370 people have browsed it

From entry to proficiency: Master the basic operations and common functions of the numpy library

The numpy library is one of the most popular scientific computing libraries in Python. It provides fast operations on multi-dimensional arrays and matrices, and supports a variety of mathematical operations, linear algebra operations, and Random number generation and other functions. Mastering the basic operations and common functions of numpy can not only improve the efficiency of data analysis and scientific computing, but also assist the development of fields such as data visualization and machine learning.

This article will introduce the basic operations and common functions of the numpy library, including the creation, indexing and slicing of numpy arrays, array operations, statistical functions, linear algebra operations and random number generation. Specific code examples are also provided to help readers get started quickly.

1. Create a numpy array

The most basic way to create a numpy array is to use the numpy.array() function, which receives a list or tuple as a parameter and creates a numpy array. The following is a simple example:

import numpy as np

a = np.array([1, 2, 3])
print(a)
Copy after login

The output result is:

[1 2 3]
Copy after login

In addition, numpy has some other functions for creating arrays, such as:

  • numpy.zeros() is used to create an array of all 0s
  • numpy.ones() is used to create an array of all 1s
  • numpy.random.rand() is used to create random numbers The array

2. Indexing and slicing

Similar to lists in Python, numpy arrays can also use indexing and slicing operations. Here are a few examples:

import numpy as np

a = np.array([1, 2, 3])
print(a[0])  # 输出1
print(a[-1])  # 输出3 

b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(b[0][1])  # 输出2

c = np.array([1, 2, 3, 4, 5, 6])
print(c[1:4])  # 输出[2 3 4]
Copy after login

Note that in numpy arrays, the slicing operation returns a view of the original array rather than a new array. Therefore, when modifications are made to the slice, the original array also changes.

3. Array operations

numpy array supports a variety of mathematical operations, such as addition, subtraction, multiplication, division, as well as polynomial functions, trigonometric functions, etc. The following are some common array operations:

import numpy as np

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

print(a + b)  # 输出[5 7 9]
print(a - b)  # 输出[-3 -3 -3]
print(a * b)  # 输出[4 10 18]
print(b / a)  # 输出[4.         2.5        2.        ]

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

print(c.dot(d))  # 矩阵乘法,输出[[ 19  22] [ 43  50]]
Copy after login

4. Statistical functions

numpy also provides a large number of statistical functions, such as sum, mean, standard deviation, maximum and minimum, etc. The following are some common statistical functions:

import numpy as np

a = np.array([1, 2, 3, 4])
print(np.sum(a))  # 求和,输出10
print(np.mean(a))  # 均值,输出2.5
print(np.std(a))  # 标准差,输出1.118033988749895
print(np.max(a))  # 最大值,输出4
print(np.min(a))  # 最小值,输出1

b = np.array([[1, 2], [3, 4]])
print(np.sum(b, axis=0))  # 按列求和,输出[4 6]
print(np.sum(b, axis=1))  # 按行求和,输出[3 7]
Copy after login

5. Linear algebra operations

numpy provides a wealth of linear algebra operation functions, such as matrix multiplication, determinant calculation, eigenvalues ​​and eigenvectors Calculation etc. The following are some common linear algebra operations:

import numpy as np

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

print(np.dot(a, b))  # 矩阵乘法,输出[[19 22] [43 50]]

c = np.array([[1, 2], [3, 4]])
print(np.linalg.det(c))  # 行列式计算,输出-2.0000000000000004

d = np.array([[1, 2], [2, 1]])
print(np.linalg.eig(d))  # 特征值和特征向量的计算,输出(array([ 3., -1.]), array([[ 0.70710678, -0.70710678], [ 0.70710678,  0.70710678]]))
Copy after login

6. Random number generation

numpy provides a variety of random functions, such as generating random integers, generating normal distributed random numbers, and generating specified shapes random array, etc. The following are some common random functions:

import numpy as np

print(np.random.randint(0, 10, 5))  # 生成5个0到10之间的随机整数,输出[1 5 8 7 3]

print(np.random.normal(0, 1, 5))  # 生成5个均值为0,方差为1的正态分布随机数,输出[-0.60690706  2.01738925 -0.58946246 -1.42619268  0.72589716]

print(np.random.rand(3, 4))  # 生成3行4列的随机数组,输出[[0.9004391  0.50630644 0.31150836 0.90425598] [0.13734967 0.53890228 0.20053875 0.00617321] [0.96756345 0.80763172 0.21116666 0.09858394]]
Copy after login

The above is an introduction and code examples of the basic operations and common functions of the numpy library. I hope this article can help readers get started quickly and become proficient in the use of numpy.

The above is the detailed content of From entry to proficiency: Master the basic operations and common functions of the numpy library. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template