Simple usage of Numpy
import numpy as np
1. Create ndarray object
Convert the list to ndarray:
>>> a = [1,2,3,4,5] >>> np.array(a) array([1, 2, 3, 4, 5])
Get a random floating point number
>>> np.random.rand(3, 4) array([[ 0.16215336, 0.49847764, 0.36217369, 0.6678112 ], [ 0.66729648, 0.86538771, 0.32621889, 0.07709784], [ 0.05460976, 0.3446629 , 0.35589223, 0.3716221 ]])
Get a random integer
>>> np.random.randint(1, 5, size=(3,4)) array([[2, 3, 1, 2], [3, 4, 4, 4], [4, 4, 4, 3]])
Get zero
>>> np.zeros((3,4)) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])
Get one
>>> np.ones((3,4)) array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])
Get empty (it’s best not to use it, find out more about it, the versions are different return The values are different)
>>> np.empty((3,4)) array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])
Take the integer zero or one
>>> np.ones((3,4),int) array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) >>> np.zeros((3,4),int) array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
Imitate the range command to create ndarray:
>>> np.arange(2,10,2) # 开始,结束,步长 array([2, 4, 6, 8])
Related recommendations: "Python Video Tutorial"
2. Viewing and operating ndarray attributes:
Look at ndarray attributes:
>>> a = [[1,2,3,4,5],[6,7,8,9,0]] >>> b = np.array(a) >>> b.ndim #维度个数(看几维) 2 >>> b.shape #维度大小(看具体长宽) (5,2) >>>b.dtype dtype('int32')
Specify attributes when creating ndarray:
>>> np.array([1,2,3,4,5],dtype=np.float64) array([ 1., 2., 3., 4., 5.]) >>> np.zeros((2,5),dtype=np.int32) array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])
Attribute transfer:
>>> a = np.array([1,2,3,4,5],dtype=np.float64) >>> a array([ 1., 2., 3., 4., 5.]) >>> a.astype(np.int32) array([1, 2, 3, 4, 5])
三, Simple operation:
Batch operation:
>>> a = np.array([1,2,3,4,5],dtype=np.int32) >>> a array([1, 2, 3, 4, 5]) >>> a + a array([ 2, 4, 6, 8, 10]) >>> a * a array([ 1, 4, 9, 16, 25]) >>> a - 2 array([-1, 0, 1, 2, 3]) >>> a / 2 array([ 0.5, 1. , 1.5, 2. , 2.5]) #等等
Change dimension:
>>> a = np.array([[1,2,3,4,5],[6,7,8,9,0]],dtype=np.int32) >>> a array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]]) >>> a.reshape((5,2)) array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]])
Matrix conversion (It is essentially different from changing dimensions, please be careful):
>>> a = np.array([[1,2,3,4,5],[6,7,8,9,0]],dtype=np.int32) >>> a array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]]) >>> a.transpose() array([[1, 6], [2, 7], [3, 8], [4, 9], [5, 0]])
Disrupt (can only disrupt one dimension):
>>> a = np.array([[1,2],[3,4],[5,6],[7,8],[9,0]],dtype=np.int32) >>> a array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]) >>> np.random.shuffle(a) >>> a array([[9, 0], [1, 2], [7, 8], [5, 6], [3, 4]])
4. Slicing and indexing:
One-dimensional array:
>>> a = np.array(range(10)) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> a[3] 3 >>> a[2:9:2] array([2, 4, 6, 8])
Multi-dimensional array:
>>> a = np.array([[1,2,3,4,5],[6,7,8,9,0],[11,12,13,14,15]],dtype=np.int32) >>> a array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 0], [11, 12, 13, 14, 15]]) >>> a[:, 1:4] array([[ 2, 3, 4], [ 7, 8, 9], [12, 13, 14]])
Conditions Index:
>>> a = np.array([[1,2,3,4,5],[6,7,8,9,0],[11,12,13,14,15]],dtype=np.int32) >>> a array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 0], [11, 12, 13, 14, 15]]) >>> a > 5 array([[False, False, False, False, False], [ True, True, True, True, False], [ True, True, True, True, True]], dtype=bool) >>> a[a>5] array([ 6, 7, 8, 9, 11, 12, 13, 14, 15]) >>> a%3 == 0 Out[128]: array([[False, False, True, False, False], [ True, False, False, True, True], [False, True, False, False, True]], dtype=bool) >>> a[a%3 == 0] array([ 3, 6, 9, 0, 12, 15])
5. Function (numpy core knowledge points)
Calculation function:
np.ceil(): 向上最接近的整数,参数是 number 或 array np.floor(): 向下最接近的整数,参数是 number 或 array np.rint(): 四舍五入,参数是 number 或 array np.isnan(): 判断元素是否为 NaN(Not a Number),参数是 number 或 array np.multiply(): 元素相乘,参数是 number 或 array np.divide(): 元素相除,参数是 number 或 array np.abs():元素的绝对值,参数是 number 或 array np.where(condition, x, y): 三元运算符,x if condition else y >>> a = np.random.randn(3,4) >>> a array([[ 0.37091654, 0.53809133, -0.99434523, -1.21496837], [ 0.00701986, 1.65776152, 0.41319601, 0.41356973], [-0.32922342, 1.07773886, -0.27273258, 0.29474435]]) >>> np.ceil(a) array([[ 1., 1., -0., -1.], [ 1., 2., 1., 1.], [-0., 2., -0., 1.]]) >>> np.where(a>0, 10, 0) array([[10, 10, 0, 0], [10, 10, 10, 10], [ 0, 10, 0, 10]])
Statistical function
np.mean():所有元素的平均值 np.sum():所有元素的和,参数是 number 或 array np.max():所有元素的最大值 np.min():所有元素的最小值,参数是 number 或 array np.std():所有元素的标准差 np.var():所有元素的方差,参数是 number 或 array np.argmax():最大值的下标索引值, np.argmin():最小值的下标索引值,参数是 number 或 array np.cumsum():返回一个一维数组,每个元素都是之前所有元素的累加和 np.cumprod():返回一个一维数组,每个元素都是之前所有元素的累乘积,参数是 number 或 array >>> a = np.arange(12).reshape(3,4).transpose() >>> a array([[ 0, 4, 8], [ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11]]) >>> np.mean(a) 5.5 >>> np.sum(a) 66 >>> np.argmax(a) 11 >>> np.std(a) 3.4520525295346629 >>> np.cumsum(a) array([ 0, 4, 12, 13, 18, 27, 29, 35, 45, 48, 55, 66], dtype=int32)
Judgment function:
np.any(): 至少有一个元素满足指定条件,返回True np.all(): 所有的元素满足指定条件,返回True >>> a = np.random.randn(2,3) >>> a array([[-0.65750548, 2.24801371, -0.26593284], [ 0.31447911, -1.0215645 , -0.4984958 ]]) >>> np.any(a>0) True >>> np.all(a>0) False
Remove duplicates:
np.unique(): 去重 >>> a = np.array([[1,2,3],[2,3,4]]) >>> a array([[1, 2, 3], [2, 3, 4]]) >>> np.unique(a) array([1, 2, 3, 4])
The above is the detailed content of Summary of simple usage of python numpy. For more information, please follow other related articles on the PHP Chinese website!

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft