程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Basic use of Python numpy

編輯:Python

About numpy

NumPy(Numerical Python) yes Python An extended library of language , Support a large number of dimension arrays and matrix operations , In addition, it also provides a large number of mathematical function libraries for array operation .
NumPy It's a scientific computing toolkit , It provides a set of functions for processing arrays , And functions for processing matrices .
NumPy It's a very fast math library , Mainly used for array calculation , contain :

  1. A powerful N Dimensional array object ndarray
  2. Broadcast function
  3. Integrate C/C++/Fortran Code tools
  4. linear algebra 、 The Fourier transform 、 Random number generation and other functions

Basic use

# numpy Use 
# Import numpy
import numpy as np
from matplotlib import pyplot as plt
# establish 2 One dimensional array 
a1 = np.array([1, 2, 3, 4, 5])
a2 = np.array([6, 7, 8, 9, 10])
# establish 2 Two dimensional array 
b1 = np.array([[1, 5], [6, 7]])
b2 = np.array([[11, 13], [18, 19]])
# Create an empty array 
c = np.empty((2, 3))
# Create a full 0 Array 
d = np.zeros((2, 3))
# Create a full 1 Array 
e = np.ones((2, 3))
# Create a random array 
f = np.random.random((2, 3))
# Create an array of random integers 
g = np.random.randint(0, 10, (2, 3))
# Create a random floating-point array 
h = np.random.rand(2, 3)
# NumPy Matrix library (Matrix)
# NumPy Provides a matrix library , Can be used to deal with matrix operations .
# NumPy The matrix library provides a matrix object , It's a two-dimensional array , Each of these elements is a floating point number .
# NumPy Matrix library provides some functions of matrix operation , Include :
# 1. The addition, subtraction, multiplication and division of matrices 
# 2. Dot product of matrix 
# 3. The transpose of the matrix 
# 4. The matrix of the inverse 
# 5. Determinant of matrix 
# 6. The rank of a matrix 
# 7. The inverse of a matrix 
# 8. The inverse of a matrix ( Use SVD)
# 9. The inverse of a matrix ( Use QR)
# 10. The inverse of a matrix ( Use LU)
# 11. The inverse of a matrix ( Use Cholesky)
import numpy.matlib
print (np.matlib.empty((2,2)))
# NumPy Linear algebra usage 
# NumPy A library of linear algebraic functions is provided linalg, This library contains all the functions needed for linear algebra 
# dot Dot product of two arrays 
np.dot(b1, b2)
print(np.dot(b1, b2))
# vdot Dot product of two vectors 
np.vdot(b1, b2)
print(np.vdot(b1, b2))
# inner The inner product of two arrays 
np.inner(b1, b2)
# matmul Matrix multiplication of two arrays 
np.matmul(b1, b2)
# determinant Determinant of matrix 
np.linalg.det(b1)
# solve Solution of matrix 
np.linalg.solve(b1, b2)
# inv The matrix of the inverse 
np.linalg.inv(b1)
# Matplotlib yes Python Drawing library of . It can be connected with NumPy Use it together , Provides an effective MatLab Open source alternatives .
# In the example ,np.arange() Function creation x The value on the axis .y The corresponding value on the axis is stored in another array object y in .
# These values use matplotlib Package's pyplot Sub module of plot() Function to draw . The figure is created by show() Function shows .
x = np.arange(1, 11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x, y)
plt.show()
# NumPy Sort 、 Conditional brush function 
# NumPy Sort :sort
# numpy.sort(a, axis, kind, order)
# a: Array to sort 
# axis: Sort the axis of the array along it , If there is no array, it will be expanded , Sort along the last axis , axis=0 Sort by column ,axis=1 Sort by row 
# kind: The default is 'quicksort'( Quick sort )
# order: If the array contains fields , The fields to be sorted 
# numpy.sort() Function returns a sorted copy of the input array 
a1 = np.array([7, 2, 0, 9, 3])
np.sort(a1)
print(np.sort(a1))
# numpy.argsort() Function returns the index value of the array value from small to large 
np.argsort(a1)
print(np.argsort(a1))
# numpy.lexsort() Used to sort multiple sequences . Think of it as a sort of spreadsheet , Each column represents a sequence , When sorting, priority is given to the next column 
# nm = ('raju','anil','ravi','amar') 
# dv = ('f.y.', 's.y.', 's.y.', 'f.y.') 
# ind = np.lexsort((dv,nm)) 
np.lexsort(a1)
print(np.lexsort(a1))
# numpy.argmax() and numpy.argmin() Function returns the index of the maximum and minimum elements along the given axis 
np.argmax(a1)
np.argmin(a1)
print(np.argmax(a1))
print(np.argmin(a1))

Relevant reference

https://www.runoob.com/numpy/numpy-tutorial.html


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved