当前位置:网站首页>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]]
边栏推荐
- [live broadcast reservation] a new challenge under the evolution of data architecture - Shanghai railway station
- VM501开发套件开发版单振弦式传感器采集模块岩土工程监测
- Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
- 2021 肯特面试题2
- Rust 入门指南(rustup, cargo)
- LabVIEW LINX Toolkit控制Arduino设备(拓展篇—1)
- 电压频率的变换原理
- 跳表的实现
- Open light input / relay output rs485/232 remote data acquisition IO module ibf70
- Summary of for loop in JS
猜你喜欢

带你来浅聊一下!单商户功能模块汇总

Summary of for loop in JS

【微信小程序开发(七)】订阅消息

2021 Yahong pen test question 2
What are the process specifications of software testing? How to do it?

Basic structure and operation principle of solar street lamp

Connection and application of portable borehole inclinometer data acquisition instrument and inclinometer probe

I'll show you a little chat! Summary of single merchant function modules

振弦采集模块测量振弦传感器的流程步骤?

A tour of gRPC:05 - gRPC server straming 服务端流
随机推荐
R语言使用GGally包的ggpairs函数可视化分组多变量的两两关系图、设置alpha参数改变图像透明度、对角线上连续变量密度图、离散变量条形图、两两关系图中包含散点图、直方图、箱图以及相关性数值
Paging query in applet
JS linked list 02
2021 Kent interview question 3
射频模块无线收发RF63U芯片应用数据传输和基建网络
振弦采集模块测量振弦传感器的流程步骤?
MLX90640 红外热成像仪测温传感器模块开发笔记(八)
记:数字累加动画
头条文章_signature
Pyqt5 rapid development and practice 5.2 container: load more controls
Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
JS queue
Open light input / relay output rs485/232 remote data acquisition IO module ibf70
Stm32f103c8t6 + 0.96 "I2C OLED display 3d_cube
Proportional solenoid valve control valve 4-20mA to 0-165ma/330ma signal isolation amplifier
跳表的实现
Why do most people who learn programming go to Shenzhen and Beijing?
Redis系列4:高可用之Sentinel(哨兵模式)
2路DI高速脉冲计数器1路编码器转Modbus TCP有线无线模块IBF161
Rust Getting Started Guide (rustup, cargo)