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
Merging Lists in Python: Choosing the Right MethodMerging Lists in Python: Choosing the Right MethodMay 14, 2025 am 12:11 AM

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

How to concatenate two lists in python 3?How to concatenate two lists in python 3?May 14, 2025 am 12:09 AM

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Python concatenate list stringsPython concatenate list stringsMay 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Python execution, what is that?Python execution, what is that?May 14, 2025 am 12:06 AM

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools