Summary of common methods in NumPy
NumPy is an open source numerical computing extension for Python. This tool can be used to store and process large matrices much more efficiently than Python's own nested list structure (which can also be used to represent matrices). NumPy (Numeric Python) provides many advanced numerical programming tools, such as matrix data types, vector processing, and sophisticated arithmetic libraries. Built for rigorous number crunching. It is mostly used by many large financial companies, as well as core scientific computing organizations such as Lawrence Livermore, and NASA uses it to handle some tasks that were originally done using C++, Fortran or Matlab.
The data type in numpy, ndarray type, is different from array.array in the standard library.
Creation of ndarray
>>> import numpy as np >>> a = np.array([2,3,4]) >>> a array([2, 3, 4]) >>> a.dtype dtype('int64') >>> b = np.array([1.2, 3.5, 5.1]) >>> b.dtype dtype('float64')
Two-dimensional array
>>> b = np.array([(1.5,2,3), (4,5,6)]) >>> b array([[ 1.5, 2. , 3. ], [ 4. , 5. , 6. ]])
Specify the type when creating
>>> c = np.array( [ [1,2], [3,4] ], dtype=complex ) >>> c array([[ 1.+0.j, 2.+0.j], [ 3.+0.j, 4.+0.j]])
Create some special matrices
>>> np.zeros( (3,4) ) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) >>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be specified array([[[ 1, 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 vary array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
Create some matrices with specific rules
>>> np.arange( 10, 30, 5 ) array([10, 15, 20, 25]) >>> np.arange( 0, 2, 0.3 ) # it accepts float arguments array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]) >>> from numpy import pi >>> np.linspace( 0, 2, 9 ) # 9 numbers from 0 to 2 array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ]) >>> x = np.linspace( 0, 2*pi, 100 ) # useful to evaluate function at lots of points >>> f = np.sin(x)
Some basic operations
Addition, subtraction, multiplication and division trigonometric functions logical operations
>>> a = np.array( [20,30,40,50] ) >>> b = np.arange( 4 ) >>> b array([0, 1, 2, 3]) >>> c = a-b >>> c array([20, 29, 38, 47]) >>> b**2 array([0, 1, 4, 9]) >>> 10*np.sin(a) array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854]) >>> a<35 array([ True, True, False, False], dtype=bool)
Matrix operations
There are in matlab. *,./etc.
But in numpy, if you use +,-,×,/, the priority is to perform addition, subtraction, multiplication and division between each point
If two matrices ( Square matrix) can perform operations between elements and perform matrix operations. Operations between elements will be performed first.
>>> import numpy as np >>> A = np.arange(10,20) >>> B = np.arange(20,30) >>> A + B array([30, 32, 34, 36, 38, 40, 42, 44, 46, 48]) >>> A * B array([200, 231, 264, 299, 336, 375, 416, 459, 504, 551]) >>> A / B array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) >>> B / A array([2, 1, 1, 1, 1, 1, 1, 1, 1, 1])
If matrix operations need to be performed, it is usually matrix multiplication
>>> A = np.array([1,1,1,1]) >>> B = np.array([2,2,2,2]) >>> A.reshape(2,2) array([[1, 1], [1, 1]]) >>> B.reshape(2,2) array([[2, 2], [2, 2]]) >>> A * B array([2, 2, 2, 2]) >>> np.dot(A,B) 8 >>> A.dot(B) 8
Some commonly used global functions
>>> B = np.arange(3) >>> B array([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.])
Matrix index slice traversal
>>> a = np.arange(10)**3 >>> a array([ 0, 1, 8, 27, 64, 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 2nd element to -1000 >>> a array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729]) >>> a[ : :-1] # reversed a array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000]) >>> for i in a: ... print(i**(1/3.)) ... nan 1.0 nan 3.0 nan 5.0 6.0 7.0 8.0 9.0
Matrix traversal
>>> import numpy as np >>> b = np.arange(16).reshape(4, 4) >>> for row in b: ... print(row) ... [0 1 2 3] [4 5 6 7] [ 8 9 10 11] [12 13 14 15] >>> for node in b.flat: ... print(node) ... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Special operations of the matrix
Change the matrix shape- -reshape
>>> a = np.floor(10 * np.random.random((3,4))) >>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]]) >>> a.ravel() array([ 6., 5., 1., 5., 5., 5., 8., 9., 5., 5., 9., 7.]) >>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]])
The difference between resize and reshape
resize will change the original matrix, reshape will not
>>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]]) >>> a.reshape(2,-1) array([[ 6., 5., 1., 5., 5., 5.], [ 8., 9., 5., 5., 9., 7.]]) >>> a array([[ 6., 5., 1., 5.], [ 5., 5., 8., 9.], [ 5., 5., 9., 7.]]) >>> a.resize(2,6) >>> a array([[ 6., 5., 1., 5., 5., 5.], [ 8., 9., 5., 5., 9., 7.]])
merge the matrices
>>> a = np.floor(10*np.random.random((2,2))) >>> a array([[ 8., 8.], [ 0., 0.]]) >>> b = np.floor(10*np.random.random((2,2))) >>> b array([[ 1., 8.], [ 0., 4.]]) >>> np.vstack((a,b)) array([[ 8., 8.], [ 0., 0.], [ 1., 8.], [ 0., 4.]]) >>> np.hstack((a,b)) array([[ 8., 8., 1., 8.], [ 0., 0., 0., 4.]])
The above is the detailed content of Summary of common methods in NumPy. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Numpy is an important mathematics library in Python. It provides efficient array operations and scientific calculation functions and is widely used in data analysis, machine learning, deep learning and other fields. When using numpy, we often need to check the version number of numpy to determine the functions supported by the current environment. This article will introduce how to quickly check the numpy version and provide specific code examples. Method 1: Use the __version__ attribute that comes with numpy. The numpy module comes with a __

How to upgrade numpy version: Easy-to-follow tutorial, requires concrete code examples Introduction: NumPy is an important Python library used for scientific computing. It provides a powerful multidimensional array object and a series of related functions that can be used to perform efficient numerical operations. As new versions are released, newer features and bug fixes are constantly available to us. This article will describe how to upgrade your installed NumPy library to get the latest features and resolve known issues. Step 1: Check the current NumPy version at the beginning

Teach you step by step to install NumPy in PyCharm and make full use of its powerful functions. Preface: NumPy is one of the basic libraries for scientific computing in Python. It provides high-performance multi-dimensional array objects and various functions required to perform basic operations on arrays. function. It is an important part of most data science and machine learning projects. This article will introduce you to how to install NumPy in PyCharm, and demonstrate its powerful features through specific code examples. Step 1: Install PyCharm First, we

The secret of how to quickly uninstall the NumPy library is revealed. Specific code examples are required. NumPy is a powerful Python scientific computing library that is widely used in fields such as data analysis, scientific computing, and machine learning. However, sometimes we may need to uninstall the NumPy library, whether to update the version or for other reasons. This article will introduce some methods to quickly uninstall the NumPy library and provide specific code examples. Method 1: Use pip to uninstall pip is a Python package management tool that can be used to install, upgrade and

Numpy installation guide: One article to solve installation problems, need specific code examples Introduction: Numpy is a powerful scientific computing library in Python. It provides efficient multi-dimensional array objects and tools for operating array data. However, for beginners, installing Numpy may cause some confusion. This article will provide you with a Numpy installation guide to help you quickly solve installation problems. 1. Install the Python environment: Before installing Numpy, you first need to make sure that Py is installed.

The NumPy library is one of the important libraries in Python for scientific computing and data analysis. However, sometimes we may need to uninstall the NumPy library, perhaps because we need to upgrade the version or resolve conflicts with other libraries. This article will introduce readers to how to correctly uninstall the NumPy library to avoid possible conflicts and errors, and demonstrate the operation process through specific code examples. Before we start uninstalling the NumPy library, we need to make sure that the pip tool is installed, because pip is the package management tool for Python.

With the rapid development of fields such as data science, machine learning, and deep learning, Python has become a mainstream language for data analysis and modeling. In Python, NumPy (short for NumericalPython) is a very important library because it provides a set of efficient multi-dimensional array objects and is the basis for many other libraries such as pandas, SciPy and scikit-learn. In the process of using NumPy, you are likely to encounter compatibility issues between different versions, then

Detailed explanation of numpy slicing operation method and practical application guide Introduction: Numpy is one of the most popular scientific computing libraries in Python, providing powerful array operation functions. Among them, slicing operation is one of the commonly used and powerful functions in numpy. This article will introduce the slicing operation method in numpy in detail, and demonstrate the specific use of slicing operation through practical application guide. 1. Introduction to numpy slicing operation method Numpy slicing operation refers to obtaining a subset of an array by specifying an index interval. Its basic form is:
