search
HomeBackend DevelopmentPython TutorialDetailed explanation of the operation of arrays by Python Numpy library

Detailed explanation of the operation of arrays by Python Numpy library

1. Introduction

NumPy (Numerical Python) is an extension library for the Python language that supports a large number of Dimensional array and matrix operations, in addition to providing a large number of mathematical function libraries for array operations. The main data structure is the ndarray array.

NumPy is often used together with SciPy (Scientific Python) and Matplotlib (plotting library), a combination widely used as a replacement for MatLab.

SciPy is an open source Python algorithm library and mathematical toolkit. SciPy includes modules for optimization, linear algebra, integration, interpolation, special functions, fast Fourier transform, signal processing and image processing, solving ordinary differential equations, and other calculations commonly used in science and engineering.

Matplotlib is a visual operating interface for the Python programming language and its numerical mathematics extension package NumPy.

2. Create

Create one-dimensional array

(1) Create directly: np.array([1, 2, 3, 4, 5, 6])

(2) Create from python list: np.array(list([1, 2, 3, 4, 5, 6]))

Create constants One-dimensional data of value

(1) Create a constant value with 0: np.zeros(n,dytpe=float/int)

(2) Create a constant value with 1 Value: np.ones(n)

(3) Create an empty array: np.empty(4)

Create an array with increasing elements

( 1) Incremental array starting from 0: np.arange(8)

(2) Given interval, custom step size: np.arange(0,1,0.2)

(3) Given an interval, customize the number: np.linspace(-1,1,50)

Create a multi-dimensional array: Create a single-dimensional array and then add it to the multi-dimensional array

# 数组的结构一定是np.array([]) 无论数组中间存放的是多少“层”数据
# 二维数组相当于存放的是“两层”数组而已
arr1=np.array(list([1, 2, 3, 4, 5]))
arr2=np.array([arr1,[1,0,0,1,0]])               # 2*5的两维数组
arr3=np.array(list([[0,0,1,1,1],[1,1,1,0,0],[2,3,4,5,6]]))    # 3*5的两维数组
arrx=np.array([arr1,list([1, 2, 3, 4, 5],[1,1,1,0,0])])     # 报错
arry=np.array([list([[ 1,2,3,  7, 11],[2,3,4,5,6]]),[1, 2, 3, 4, 5]]) # 报错

Related recommendations: "Python Video Tutorial"

Create (n*m)-dimensional data with constant values

(1) Create a constant value of 0: np.zeros((n*m),dytpe=float/int)

(2) Create a constant value with 1: np.ones((n*m))

(3 )Create an empty array: np.empty((n*m))

Create an array of random numbers

Generate a random number seed:

(1) np.random.seed()

(2) np.random.RandomState()

Generate random numbers:

Detailed explanation of the operation of arrays by Python Numpy library

Generates yes Random array with regular distribution

(1) Binomial distribution: np.random.binomial(n, p, size)

(2) Normal distribution: np.random.normal(loc , scale, size)

Convert csv files into arrays or arrays

Use np.genfromtxt('csv file name', delimiter = 'delimiter in the file') function Convert the file into an array

 csv_array = np.genfromtxt('sample.csv', delimiter=',')
 print(csv_array)

3. Transformation of the array

Generates the function of array/matrix transposition, that is, the exchange of row and column numbers, use .T

a = np.array([[32, 15, 6, 9, 14], 
              [12, 10, 5, 23, 1],
              [2, 16, 13, 40, 37]])
print(a.T)
-------------------
# 结果如下
[[32 12  2]
 [15 10 16]
 [ 6  5 13]
 [ 9 23 40]
 [14  1 37]]

Change the shape of the array:

(1) arr.resize(n,m): The arr.resize(n,m) function modifies the array in place, requiring: the number of elements must be consistent

a=np.arange(8)
a.resize(2,4)
print(a)
---------------------------
[[0 1 2 3]
 [4 5 6 7]]

(2) arr.reshape(n,m): If the parameter of a certain dimension is -1, it means that the total number of elements will be calculated based on the other dimension.

a=np.arange(8).reshape(-1,1)
print(a)
-----------------
[[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]]

Will one Raising the dimension to two dimensions: np.newaxis

np.newaxis actually means directly increasing the dimension. We generally do not add too many dimensions to the array. Here is an example of increasing one dimension to two dimensions:

(1) Increase the row dimension: arr[np.newaxis, :]

(2) Increase the column dimension: arr[:, np.newaxis]

a=np.arange(8)
a             # array([0, 1, 2, 3, 4, 5, 6, 7])
a.shape           # (8,)
a[np.newaxis, :]      # array([[0, 1, 2, 3, 4, 5, 6, 7]])
a.shape           # (8,)
a[: , np.newaxis]     # array([[0],[1],[2],[3],[4],[5],[6],[7]])
a.shape           # (8,)

Dimensionality reduction : arr.ravel()

arr.ravel() function when reducing dimensions: the default is to generate a new array in row order (that is, read line by line); if the parameter "F" is passed in, the column order is reduced Dimensions generate new array

a=np.array([[1,2],[3,4]])
a.ravel()       
a.ravel('F')      
----------------------------
# 结果 array([1, 2, 3, 4])
# 结果 array([1, 3, 2, 4])

4. Calculation

Perform calculation operations on arrays

(1) Add and subtract elements

a=np.arange(8).reshape(2,4)       # array([[0, 1, 2, 3], [4, 5, 6, 7]])
b=np.random.randint(8,size=(2,4))   # array([[1, 2, 5, 3], [4, 1, 0, 6]])
a+b
a-b
----------------------------
# a+b和a-b结果分别是:
array([[ 1,  3,  7,  6],
       [ 8,  6,  6, 13]])
array([[-1, -1, -3,  0],
       [ 0,  4,  6,  1]])

(2) Multiplication: square/multiply the elements in the matrix

a=np.arange(8).reshape(2,4)       # array([[0, 1, 2, 3], [4, 5, 6, 7]])
b=np.random.randint(8,size=(2,4))   # array([[1, 2, 5, 3], [4, 1, 0, 6]])
a**2
a*b
-----------------------
# a矩阵平方/a*b矩阵中元素相乘结果分别:
array([[ 0,  1,  4,  9],
       [16, 25, 36, 49]])
array([[ 0,  2, 10,  9],
       [16,  5,  0, 42]])

(3) Matrix*matrix:

# 要求a矩阵的行要等于b矩阵的列数;且a矩阵的列等于b矩阵的行数
a=np.arange(8).reshape(2,4)       # array([[0, 1, 2, 3], [4, 5, 6, 7]])
b=np.random.randint(8,size=(4,2))   # array([[3, 0],[3, 3],[5, 6],[6, 7]])
c1 = np.dot(a,b)
c2 = a.dot(b)
----------------------
# ab矩阵相乘的结果:c1=c2 
array([[ 31,  36],
     [ 99, 100]])

(4) Logical calculation

[Note] The list cannot be used as a whole to make logical judgments on the individual elements in it!

# 结果返回:一个数组,其中每个元素根据逻辑判断的布尔类型的结果
a > 3 
-----------------------------
# 结果如下:
array([[False, False, False, False],
     [ True,  True,  True,  True]])

5. Value

Get an element in a one-dimensional array: The operation is the same as the index of the list list

a = np.array([5, 2, 7, 0, 11])
a[0]      # 结果为 5
a[:4]     # 结果为 从头开始到索引为4结束
a[2:]     # 结果为 从索引为2的开始到结尾
a[::2]      # 结果为 从头开始到结尾,每2个取一个值

Get a multi-dimensional array An element, a row or a column value

a = np.array([[32, 15, 6, 9, 14], 
         [12, 10, 5, 23, 1],
         [2, 16, 13, 40, 37]])
a[2,1]     # 结果是一个元素 16
a[2][1]    # 结果是一个元素 16
a[1]      # 第2行 array([12, 10,  5, 23,  1])
a[:,2]   # 取出全部行,第2列 [15,10,16]
a[1:3, :]   # 取出[1,3)行,全部列
a[1,1:]    # array([10,  5, 23,  1])

Get the

# 需要注意的是,我们数据进行逻辑计算操作得到的仍然是一个数组
# 如果我们想要的是一个过滤后的数组,就需要将"逻辑判断"传入数组中
a = np.array([[32, 15, 6, 9, 14], 
              [12, 10, 5, 23, 1],
              [2, 16, 13, 40, 37]])
a[a > 3]
a[(a > 3) | (a < 2)]  
------------------------------
# 结果分别是:
array([32, 15,  6,  9, 14, 12, 10,  5, 23, 16, 13, 40, 37])
array([32, 15,  6,  9, 14, 12, 10,  5, 23,  1, 16, 13, 40, 37])

that satisfies the logical operation Traversal: the result is output in rows

a = np.array([[32, 15, 6, 9, 14], 
         [12, 10, 5, 23, 1],
         [2, 16, 13, 40, 37]])
for x in a:
    print(x)
--------------------
[32 15  6  9 14]
[12 10  5 23  1]
[ 2 16 13 40 37]

6. Copy/ Split/Merge

Copy: arr.cope()

Split:

(1) Equal parts: np.split(arr, n, axis=0 /1) (That is, only when the number of rows or columns can be divided evenly by n)

(2) Unequal division: np.array_split(arr, n) Default is divided into n parts by row

a = np.array([[32, 15, 6, 9, 14, 21], 
         [12, 10, 5, 23, 1, 10],
         [2, 16, 13, 40, 37, 8]])
              
# 可以看到a矩阵是(3*6),所以使用np.split()只能尝试行分成3份;或者列分成2/3/6份 
np.split(a,3,axis=0)  
np.split(a,3,axis=1)
np.array_split(a,2)
np.array_split(a,4,axis=1)
-------------------------------------------
[array([[32, 15,  6,  9, 14, 21]]),
 array([[12, 10,  5, 23,  1, 10]]),
 array([[ 2, 16, 13, 40, 37,  8]])]
   
[array([[32, 15],
        [12, 10],
        [ 2, 16]]), array([[ 6,  9],
        [ 5, 23],
        [13, 40]]), array([[14, 21],
        [ 1, 10],
        [37,  8]])]
        
[array([[32, 15,  6,  9, 14, 21],
        [12, 10,  5, 23,  1, 10]]), array([[ 2, 16, 13, 40, 37,  8]])]
        
[array([[32, 15],
        [12, 10],
        [ 2, 16]]), array([[ 6,  9],
        [ 5, 23],
        [13, 40]]), array([[14],
        [ 1],
        [37]]), array([[21],
        [10],
        [ 8]])]


Merge: np.concatenate((arr1, arr2, arr3), axis=0/1) Default is connected to the data

a=np.random.rand(2,3)
b=np.random.randint(1,size=(2,3))
np.concatenate((a,b,a))         # 接在下面
np.concatenate((a,b,a),axis=1)      # 接在后面
------------------------
array([[0.95912866, 0.81396527, 0.809493  ],
       [0.4539276 , 0.24173315, 0.63931439],
       [0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        ],
       [0.95912866, 0.81396527, 0.809493  ],
       [0.4539276 , 0.24173315, 0.63931439]])
array([[0.95912866, 0.81396527, 0.809493  , 0.        , 0.        ,
        0.        , 0.95912866, 0.81396527, 0.809493  ],
       [0.4539276 , 0.24173315, 0.63931439, 0.        , 0.        ,
        0.        , 0.4539276 , 0.24173315, 0.63931439]])

The above is the detailed content of Detailed explanation of the operation of arrays by Python Numpy library. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.