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

How Python uses the Numpy Library

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "Python how to use the Numpy library", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Python how to use the Numpy library" this article.

Introduction to Numpy Library

The most basic data type handled by the numpy library is a multidimensional array (ndarray) made up of the same elements, referred to as an "array". All elements in the array must be of the same type, and the elements in the array can be indexed by integers with ordinal numbers starting at 0. The dimensions of ndarray type is called axes, and the number of axes is called rank. The rank of one-dimensional array is 1, and the rank of two-dimensional array is 2. Two-dimensional array is equivalent to two one-dimensional arrays.

Overview of the numpy library:

Because there are many functions in the numpy library and naming is easy to be confused with common naming, it is recommended to reference the numpy library in the following ways:

Import numpy as np

Among them, the use of the as reserved word with import can change the namespace of the library in subsequent code, which helps to improve the readability of the code. To put it simply, np replaces numpy in subsequent parts of the program.

Common properties of the ndarray class

After creating a simple array, you can see that the ndarray type has some basic properties

1.ndarray.ndim

The number of axes in an array. In python's world, the number of axes is called rank.

2.ndarray.shape

The dimension of the array. This is an integer tuple that indicates the size of the array on each dimension. For example, for a matrix with n rows and m columns, its shape attribute will be (2p3), and the length of this tuple is obviously rank, that is, dimension or ndim attribute.

3.ndarray.size

The total number of array elements equal to the product of tuple elements in the shape attribute.

4.ndarray.dtype

An object that describes the type of elements in an array. You can use the standard Python type by creating or specifying a dtype. In addition, NumPy provides its own data types.

5.ndarray.itemsize

The byte size of each element in the array. For example, an array of element type float64 has an itemsiz attribute of 8 (= 64 complex32), and an array of element type item has an attribute of 4 (= 32 amp 8).

6.ndarray.data

The buffer that contains the elements of the actual array, usually we do not need to use this attribute, because we always use the elements in the array through the index.

Import numpy as np # introduces numpy module a = np.ones (4Jing 5)) print (a) print ('number of data axes:', a.ndim) print ('number of data per dimension:', a.shape) print ('data type', a.dtype)

# result

[[1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1.]]

Number of data axes: 2

Number of data per dimension: (4, 5)

Data type float64

Summary:

Numpy common function? array function

Function prototype:

Np.array (dtype=int) # creates arrays from lists and tuples

Function: array function converts the list to matrix

Import numpy as np # introduces the numpy module array=np.array ([[1Magne 2jue 3], # list transfer matrix [4Magazine 6]]) print (array) print ('number of dim',array.ndim) # output matrix data axis number print (' size',array.size) # total number of output matrix elements

# result

[[1 2 3]

[4 5 6]]

Number of dim 2

Size 6

? arange () function and linspace () function

Function prototype:

Np.arange # create an array of x to y, in steps of I (np.linspace) # create an array of x to y, equally divided into n elements import numpy as np # introduces the numpy module a=np.arange (1LJ 12) # np.arange from 1 to 12, interval 2print (a) # from 2 to 12, generate 6 random numbers and control them into 2 rows and 3 columns b=np.linspace (2line 12Co6). Reshape (2line 3) print (b)

# results:

[1 3 5 7 9 11]

[[2. 4. 6.]

[8. 10. 12.]]

? zeros (), empty and ones ()

Function: create an array of elements with the same initial value (0Magne1)

Function prototype:

Np.ones ((mforce n), dtype) # create an array of m rows and n columns of all ones np.zeros ((m line n), dtype) # create an array of m rows and n columns of all zeros of np.empty ((m line n) Dtype) # create an array of m rows and n columns with all zeros. Import numpy as np # introduces the numpy module a=np.zeros ((5Power8)) # elements are all 0b=np.ones ((5Power8)) # elements are all 1print (a) print (b)

# results:

[[0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0. 0.]]

[[1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1.]

[1. 1. 1. 1. 1. 1.]]

Indexing and slicing methods for ndarray classes

It's similar to the index of the list.

Import numpy as np # introduces the numpy module a = np.random.rand (5Magne3) # to generate a random five-row, three-column array print (a) print ('get the second row', a [2]) # get the second row print ('slice', a [1:3]) # slices, 1 to 3 rows, excluding 3print ('slices', a [- 5lue 2vv2]) # slices, from back to front, step size 2

# result

[[0.53469047 0.47559129 0.65865181]

[0.89942399 0.66683114 0.55181635]

[0.11989817 0.06055933 0.56880058]

[0.95744499 0.94814163 0.2155053]

[0.95179242 0.61544664 0.40876683]]

Get line 2 [0.11989817 0.06055933 0.56880058]

Slice [0.89942399 0.66683114 0.55181635]

[0.11989817 0.06055933 0.56880058]]

Slice [0.53469047 0.47559129 0.65865181]

[0.11989817 0.06055933 0.56880058]]

Numpy library operation function

Summary

Tip: here is a summary of the article:

Numpy library also includes trigonometric operation functions, Fourier transform, random and probability distribution, basic numerical statistics, bit operation, matrix operation and other very rich functions, which can be queried on the official website when in use.

Python basic look-up table

These are all the contents of the article "how Python uses the Numpy Library". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report