Master common numpy functions and their applications: learn the basic knowledge of numpy functions

王林
Release: 2024-01-26 08:46:06
Original
528 people have browsed it

Master common numpy functions and their applications: learn the basic knowledge of numpy functions

Learn numpy functions: Master common numpy functions and their usage, specific code examples are required

Python is a powerful programming language that is widely used in data analysis and The field of scientific computing. In this field, numpy is a very important library, which provides a large number of functions for working with arrays and matrices. In this article, we will explore some commonly used numpy functions and their usage, and provide concrete code examples.

First, we need to import the numpy library to use its functions. Before importing, make sure you have correctly installed the numpy library. You can install numpy using the following command:

pip install numpy
Copy after login

Once you have successfully installed numpy, you can import it in your code:

import numpy as np
Copy after login

Next, let’s start learning a few commonly used Numpy functions and their usage.

  1. Creating Arrays
    numpy provides a variety of ways to create arrays. The easiest way is to use the np.array function. The following code example creates a one-dimensional array:
a = np.array([1, 2, 3, 4, 5])
print(a)
Copy after login

Output result:

[1 2 3 4 5]
Copy after login

In addition to using the np.array function, you can also create an array using the following methods :

  • np.zeros: Create an array filled with 0s;
  • np.ones: Create an array filled with 1s Array;
  • np.arange: Create an array of equal intervals;
  • np.linspace: Create an array of equal intervals;
  1. Array operations
    numpy provides many functions for operating arrays. Below are some common functions and their usage.
  • np.shape: Get the shape of the array;
  • np.ndim: Get the dimensions of the array;
  • np.size: Get the size of the array;
  • np.reshape: Change the shape of the array;
  • np .concatenate: Concatenate two arrays;
  • np.split: Divide an array into multiple sub-arrays;

The following code example demonstrates some Usage of array operations:

a = np.array([[1, 2, 3], [4, 5, 6]])
print(np.shape(a))  # 输出(2, 3)
print(np.ndim(a))  # 输出2
print(np.size(a))  # 输出6

b = np.reshape(a, (3, 2))
print(b)
Copy after login

Output results:

[[1 2]
 [3 4]
 [5 6]]
Copy after login
  1. Mathematical operations
    numpy provides a wealth of mathematical functions for calculating arrays. Here are some common mathematical functions and their uses.
  • np.sum: Calculate the sum of array elements;
  • np.mean: Calculate the average of array elements ;
  • np.max: Find the maximum value in the array;
  • np.min: Find the minimum value in the array;
  • np.sin: Calculate the sine value of the array element;
  • np.cos: Calculate the cosine value of the array element;

The following code examples demonstrate the use of some mathematical operations:

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

print(np.sum(a))  # 输出15
print(np.mean(a))  # 输出3.0
print(np.max(a))  # 输出5
print(np.min(a))  # 输出1

b = np.sin(a)
print(b)
Copy after login

Output results:

[ 0.84147098  0.90929743  0.14112001 -0.7568025  -0.95892427]
Copy after login
  1. Matrix operations
    In addition to performing mathematical operations on arrays, numpy also provides a wealth of matrix operation function. The following are some common matrix operation functions and their usage.
  • np.dot: Calculate the dot product of two matrices;
  • np.transpose: Matrix transpose;
  • np.linalg.inv: Calculate the inverse of the matrix;
  • np.linalg.det: Calculate the determinant of the matrix;
  • np.linalg.solve: Solve a system of linear equations;

The following code example demonstrates the use of some matrix operations:

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

c = np.dot(a, b)
print(c)

d = np.transpose(a)
print(d)

e = np.linalg.inv(a)
print(e)

f = np.linalg.det(b)
print(f)

x = np.array([[1, 2], [3, 4]])
y = np.array([5, 6])
z = np.linalg.solve(x, y)
print(z)
Copy after login

Output results:

[[19 22]
 [43 50]]
[[1 3]
 [2 4]]
[[-2.   1. ]
 [ 1.5 -0.5]]
-2.000000000000002
[-4.   4.5]
Copy after login

In this article, we introduce some commonly used numpy functions and their usage. By mastering these functions, you will be able to manipulate arrays and matrices more flexibly and perform various mathematical and scientific calculations. I hope this article will help you learn numpy functions!

The above is the detailed content of Master common numpy functions and their applications: learn the basic knowledge of numpy functions. 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