Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the basic knowledge of Python numpy

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the introduction of the basic knowledge of Python numpy. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the basic knowledge of Python numpy.

1. Basic knowledge

The main objects of NumPy are multidimensional arrays. It is a table made up of the same elements (usually numbers) and indexed by positive integer tuples (tuple).

In an array, dimensional is called axis, and the number of axes is called rank, as in the following array, which has two axes (axis), the first latitude (dimension, or axis axis) is 2 (vertical), and the second latitude length is three (horizontal).

[[1., 0., 0.], [0., 1., 2.]]

The array class of Numpy is called ndarray, alias array. Note that numpy.array is not the same as array.array in the Python standard library, where array provides only one-dimensional arrays and few functions.

1.1 attributes of the array

The main properties of the ndarray object are:

Number of latitudes or axes of ndarray.ndim

The size of each latitude of the ndarray.shape array

Total number of elements in the ndarray.size array

Types of elements in the ndarray.dtype array

The size of the binary of ndarray.itemsize array elements

Ndarray.data array element container (not commonly used)

> > import numpy as np > > a = np.arange (15). Reshape (3,5) > aarray ([[0,1,2,3,4], [5,6,7,8,9], [10,11,12,13,14]]) > > a.shape (3,5) > > a.ndim2 > a.dtype.nameplate int64'> > a.itemsize8 > > a.size15 > > type (a) > > b = np.array ([6,7,8]) > > barray ([6,7,8]) > > type (b)

1.2 create an array

Create arrays through lists (list) or tuples (tuple) and array () functions. The type of the element is determined according to the type of the element in the sequence.

> import numpy as np > a = np.array > aarray > a.dtypedtype ('int64') > b = np.array ([1.2,3.5,5.1]) > b.dtypedtype (' float64')

The array will convert the sequence of the sequence into a two-dimensional array and the sequence of the sequence into a three-dimensional array.

> b = np.array ([(1.5, 5, 6)]) > barray ([[1.5, 5, 6]))

You can set the type of the array in advance

> c = np.array ([[1recover2], [3jr4]], dtype=complex) > carray ([1.0j, 2.0j], [3.00.j, 4.0j]]))

NumPy provides functions for creating arrays that contain placeholders so that you can initialize arrays whose contents are not predetermined.

> > np.zeros ((3p4)) array ([[0.,0.,0.,0.], [0.0.0.0.0.0], [0.0.0.0.0.0]]) > np.ones ((2Jing 3Jing 4), dtype=np.int16) # dtype can also be specifiedarray ([1,1,1,1], [1,1,1,1], [1,1,1,1]), [[1,1,1,1] [1, 1, 1, 1], [1, 1, 1]], dtype=int16) > > np.empty (2) 3)) # uninitialized, output may varyarray ([[3.73603959e-262, 6.02658058e-154, 6.55490914e-260], [5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])

NumPy provides a function similar to range (), arange (), to create sequence arrays directly.

> np.arange (10,30,5) array ([10,15,20,25]) > np.arange (0,2,0.3) # it accepts float argumentsarray ([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])

Similarly, we can also use the linspace () function to create, note the differences in the parameters

> from numpy import pi > np.linspace (0,2,9) # 9 numbers from 0 to 2array ([0. , 0.25,0.5,0.75, 1., 1.25,1.5,1.75,2.) > x = np.linspace

1.3 print array

Print arrays follow the following three rules:

1) the last axis is printed from left to right

2) the penultimate axis is printed from top to bottom

3) the rest of the axis is also from top to bottom, and each segment is separated by a blank line

> a = np.arange (6) # 1D array > print (a) [012345] > b = np.arange (12). Reshape # 2d array > print (b) [0 12] [3 45] [6 7 8] [9 10 11] > c = np.arange (24). Reshape (2p3) 4) # 3D array > print (c) [[0 12 3] [4 5 6 7] [8 9 10 11] [[12 13 14 15] [16 17 18 19] [20 21 22 23]

If the array is too large, NumPy skips the data in the middle, and if you don't want to do so, you need to set print options

> np.set_printoptions (threshold='nan')

1.4 basic operation

Arithmetic operations are applied to elements. The result is output as a new array.

> a = np.array > > b = np.arange (4) > barray ([0,1,2,3]) > > c = carray ([20,29,38,47]) > b**2array ([0,1,4,9]) > 10*np.sin (a) array ([9.12945251,-9.88031624, 7.4511316,-2.62374854]) > > a > > A = np.array ([[1,4,9])] ) > B = np.array ([[2mem0],... [3mem4]]) > A.dot B # elementwise productarray ([[2mem0], [0,4]) > A.dot (B) # matrix productarray ([5,4], [3pyr4]) > np.dot (A, B) # another matrix productarray ([[5,4], [3mem4]))

Some operations, such as + =, * =, change themselves without generating a new array:

> > a = np.ones ((2p3), dtype=int) > > b = np.random.random ((2p3)) > > a * = 3 > aarray ([[3,3,3], [3,3,3]]) > > b + = a > barray ([[3.417022, 3.72032449, 3.00011437], [3.30233257, 3.14675589, 3.09233859]])

When two different types of arrays operate, the results will be consistent with more general or more accurate arrays.

> > a = np.ones (3, dtype=np.int32) > > b = np.linspace > > b.dtype.nameplate float64'> > c = afigb > carray ([1., 2.57079633, 4.14159265]) > > c.dtype.nameboat float64' > > d = np.exp (cym1j) > darray ([0.54030231cm 0.84147098j,-0.84147098j 0.54030231j procrastination 0.54030231-0.84147098j]) > > d.dtype.nameplate complex128j) > >

Ndarray can also perform unary operations:

> a = np.random.random ((2Power3)) > aarray ([[0.18626021, 0.34556073, 0.39676747], [0.53881673, 0.41919451, 0.6852195]]) > a.sum () 2.5718191614547998 > a.min () 0.1862602113776709 > > a.max () 0.6852195003967595

By default, these operations treat the entire array as a list, ignoring its latitude. However, you can specify the axis (axis).

> > b = np.arange (12). Reshape (3p4) > > barray ([[0,1,2,3], [4,5,6,7], [8,9,10,11]]) > b.sum (axis=0) # sum of each columnarray ([12,15,18,21]) > b.min (axis=1) # min of each rowarray ([0,4,8]) > b.cumsum (axis=1) # cumulative sum along each rowarray ([[0,1,3,6], [4,9,15]) 22], [8, 17, 27, 38])

1.5 Universal function

NumPy provides commonly used mathematical functions such as sin, cos, exp, which will return an array after operation.

> B = np.arange (3) > Barray ([0,1,2]) > np.exp (B) array ([1., 2.71828183, 7.3890561]) > np.sqrt (B) > array ([0. , 1, 1.41421356]) > > C = np.array ([2,-1, 4.]) > np.add (B, C) array ([2, 0, 6.])

1.6 indexing, slicing, and iteration

One-dimensional arrays operate just like other python sequences such as list.

> > a = np.arange (10) * * 3 > aarray ([0,1,8,27,64,125,125,216,343,512,729]) > > a [2] 8 > > a [2:5] array ([8,27,64]) > > a [: 6:2] =-1000 # equivalent to a [0:6:2] =-1000 From start to position 6, exclusive, set every > aarray ([- 1000, 1,-1000, 27,-1000, 125,216,343,5127,729]) > > a [::-1] # reversed aarray ([729,512,343,216,125,-1000, 27,-1000, 1,-1000]) > > for i in. Print (iTunes * (1Compact 3.))... nan1.0nan3.0nan5.06.07.08.09.0

Multidimensional arrays can be indexed by different axes.

> def f (XBI y):... Return 10 classy. > > b = np.fromfunction (f, (5d4), dtype=int) > barray ([0, 1, 2 each row in the second column of barray 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]) > > b [2p3] 23 > > b [0:5, 1] # each row in the second column of barray ([1,11,21,31,41]) > > b: 1] # equivalent to the previous examplearray ([1, 11, 21, 31, 41]) > > b [1:3,:] # each column in the second and third row of barray ([10, 11, 12, 13], [20, 21, 22, 23])

Iterator:

> for row in bvv. Print (row)... [0 12 3] [10 11 12 13] [20 21 22 23] [30 31 32 33] [40 41 42 43]

If you want to iterate over each element, you can use the flat attribute:

> Print (element).. 01231011121320212233031323340414243 Thank you for your reading. The above is the content of "introduction to the basic knowledge of Python numpy". After the study of this article, I believe you have a deeper understanding of the introduction of the basic knowledge of Python numpy, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report