当前位置:网站首页>Numpy ndarray learning < I > Foundation
Numpy ndarray learning < I > Foundation
2022-07-28 16:17:00 【New_ Tank】
0. ndarray structure :
numpy One of its characteristics is N Dimension group --ndarray.
ndarray Is a collection of data of the same type , Each of these elements has the same storage size area in memory .
ndarray Inside , It consists of the following parts :
A. A pointer to data .
B. Element data type or dtype. Used to determine the size of each element in memory .
C. A said ndarray shape (shape) tuples . Used to determine the size of each dimension .
D. One span (stride) Tuples . Used to determine the step size to span to the next element .
1. ndarray establish :
To create a ndarray. Just call np.array():
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
| name | describe |
|---|---|
| object | An array or nested sequence of numbers |
| dtype | Data type of array element , Optional |
| copy | Whether the object needs to be copied , Optional |
| order | Create an array style ,C In the direction of the line ,F For column direction ,A In any direction ( Default ) |
| subok | By default, it returns an array consistent with the base class type |
| ndmin | Specifies the minimum dimension of the generated array |
import numpy as np
arr1 = np.array([3,4,5])
print("arr1:",arr1)
print("type:",type(arr1))
print("shape:",arr1.shape)
arr2 = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9],[10, 11, 12]])
print("arr2:",arr2)
print("type:",type(arr2))
print("shape:",arr2.shape)
arr3 = np.array([1, 2, 3], ndmin= 3)
print("arr3:",arr3)
print("type:",type(arr3))
print("shape:",arr3.shape)
arr1: [3 4 5] type: <class 'numpy.ndarray'> shape: (3,) arr2: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] type: <class 'numpy.ndarray'> shape: (4, 3) arr3: [[[1 2 3]]] type: <class 'numpy.ndarray'> shape: (1, 1, 3)
2. ndarry The index of :
The index is obtained through an unsigned integer ndarray Value .
Yes, one-dimensional ndarray, A single index value represents an element scalar .
For two-dimensional ndarray, A single index value represents the corresponding one-dimensional ndarray.
Yes N dimension ndarray, A single index value represents the corresponding (N-1) dimension ndarray.
arr1 = np.array([3,4,5])\
print(arr1[1],"\n")
4
arr2 = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9],[10, 11, 12]])
print(arr2[1], "\n")
[4 5 6]
print(arr2[1][2])
6
Because since arr2[1] It's a one-dimensional ndarray. that arr2[1][2] It's this one-dimensional array [4,5,6] Of the index=2 individual . That is to say 6.
The more common method is :
print(arr2[1,2])
6
3. ndarry The section of :
4. ndarray Of min(), max(), sum() etc. :
ndarray.min(axis=None, out=None, keepdims=False, initial=<no value>, where=True)
ndarray.max(axis=None, out=None, keepdims=False, initial=<no value>, where=True)
ndarray.sum(axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True)
They are equivalent to :
numpy.amin(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
numpy.sum(a, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
They are :
Return minimum -- Along the given axis
Return maximum -- Along the given axis.
Returns the sum of elements -- Along the given axis.
arr4 = np.array([[1, 2, 3], [4, 5, 6]])
print("min()", arr4.min())
print("min(0)", arr4.min(0))
print("min(1)", arr4.min(1))
axis:
If you don't choose . Is to find the minimum value in the whole field , Maximum , And the value .
If elected 0. Then it means to find the minimum value on this axis , Maximum , And the value .
If it is shape=(m, n). The result is shape(1,n) Of ndarray.
arr4 = np.array([[1, 2, 3], [4, 5, 6]])
print("min()", arr4.min())
print("min(0)", arr4.min(0))
print("min(1)", arr4.min(1))
min() 1 min(0) [1 2 3] min(1) [1 4]
print("max() :", arr4.max())
print("max(0):", arr4.max(0))
print("max(1):", arr4.max(1))
max() : 6 max(0): [4 5 6] max(1): [3 6]
5. ndarray Addition, subtraction, multiplication and division :
Arithmetic functions contain simple addition, subtraction, multiplication and division : add(),subtract(),multiply() and divide()
It can also be used directly “+” , “-” , “*” , "/" To calculate .
such , be ndarray Add, subtract, multiply and divide the corresponding elements of .
arr5 = np.array([[0, 1, 2], [3, 4, 5]])
arr6 = np.array([[2, 2, 2], [2, 2, 2]])
print("Add:")
print(arr5 + arr6)
print(np.add(arr5, arr6))
print("Sub:")
print(arr5 - arr6)
print(np.subtract(arr5, arr6))
print("Mul:")
print(arr5 * arr6)
print(np.multiply(arr5, arr6))
print("Div:")
print(arr5 / arr6)
print(np.divide(arr5, arr6))
Add: [[2 3 4] [5 6 7]] [[2 3 4] [5 6 7]] Sub: [[-2 -1 0] [ 1 2 3]] [[-2 -1 0] [ 1 2 3]] Mul: [[ 0 2 4] [ 6 8 10]] [[ 0 2 4] [ 6 8 10]] Div: [[0. 0.5 1. ] [1.5 2. 2.5]] [[0. 0.5 1. ] [1.5 2. 2.5]]
5.1: After the broadcast ndarray Calculation .
5.2:ndarray And integer calculation :
First broadcast integers , Convert it into and ndarray Of shape identical . Calculate again .
6. Matrix multiplication :
stay numpy in , have access to numpy.ndarray Two dimensional pattern representation matrix . You can also use matrix According to matrix . But recommended ndarray.
Matrix multiplication :
If ndarray by 2 Dimensional , be numpy.dot() and np.matmul() The result of the function is the same . Are matrix multiplication .
from Python 3.5 and Numpy 1.10 in the future , You can use infix operators @ Calculation ndarray Matrix multiplication between , It's easier to write .
The following is a detailed introduction to np.dot()
numpy.dot(a, b, out=None)
If a and b They're all one-dimensional arrays , Then it is the inner product of the vector . ( Corresponding bit multiplication , Then I add )
If a and b It's all two-dimensional arrays , Matrix multiplication , But preferred matmul or .a @ b
If a or b yes 0-D( Scalar ), Equivalent to multiply . And we recommend numpy.multiply(a, b) or a * b.( see 5.2)
Examples of matrix multiplication rules :
a = np.array([[1,2],[3,4]]) b = np.array([[11,12],[13,14]]) print(np.dot(a,b))
The output is :
[[37 40] [85 92]]
The formula is :
[[1*11+2*13, 1*12+2*14],[3*11+4*13, 3*12+4*14]]
边栏推荐
- Stm32f103c8t6 + 0.96 "I2C OLED display 3d_cube
- 兆骑科创创新创业大赛人才引进平台,双创赛事高层次人才引进
- 记项目 常用js方法
- Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
- Rust 入门指南(rustup, cargo)
- High speed counter to rs485modbus RTU module ibf150
- Installation points and precautions of split angle probe
- js 链表 01
- 便携式钻孔测斜仪数据采集仪测量原理与测斜探头的连接及使用方法
- 太阳能路灯的根本结构及作业原理
猜你喜欢

射频模块无线收发RF63U芯片应用数据传输和基建网络

5-channel di/do relay output remote IO acquisition module Modbus tcp/ibf95

2021 亚鸿笔试题

2021 亚鸿笔试题2

远距离串口服务器( 适配器)UART 转 1-Wire 应用

Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)

A tour of gRPC:05 - gRPC server straming 服务端流

High speed counter to rs485modbus RTU module ibf150

LabVIEW LINX Toolkit控制Arduino设备(拓展篇—1)

太阳能路灯的根本结构及作业原理
随机推荐
MicTR01 Tester 开发套件(振弦采集读数仪)使用说明
2021 肯特面试题3
Have you seen the management area decoupling architecture? Can help customers solve big problems
李宏毅《机器学习》丨4. Deep Learning(深度学习)
太阳能路灯的根本结构及作业原理
A failed cracking experience
记录一下 clearfix 清除浮动
Telecommuting can be easily realized in only three steps
我在上海偶遇数字凤凰#坐标徐汇美罗城
NTC, PT100 thermal resistance to 4-20mA temperature signal converter
Mlx90640 infrared thermal imager temperature sensor module development notes (VIII)
How to quickly access the unified authentication system
Basic structure and operation principle of solar street lamp
R语言使用fs包的file_delete函数删除指定文件夹下的指定文件、举一反三、dir_delete函数、link_delete函数可以用来删除文件夹和超链接
Note: the value is rounded up to ten, hundred, thousand, ten thousand
JS array (summary)
js 队列
Remote serial port server (adapter) UART to 1-wire application
远距离串口服务器( 适配器)UART 转 1-Wire 应用
2021 Kent interview question 1