numpy库概述
numpy库处理的最基础数据类型是由同种元素构成的多维数组,简称为“数组”
数组的特点:
- 数组中所有元素的类型必须相同
- 数组中元素可以用整数索引
- 序号从0开始
ndarray类型的维度叫做轴,轴的个数叫做秩
numpy库的解析
由于numpy库中函数较多而且容易与常用命名混淆,建议采用如下方法引用numpy库
1
|
import numpy as np |
numpy库中常用的创建数组函数
函数 | 描述 |
np.array([x,y,z],dtype=int) | 从Python列表和元组中创建数组 |
np.arange(x,y,i) | 创建一个由x到y,以i为步长的数组 |
np.linspace(x,y,n) | 创建一个由x到y,等分成n个元素的数组 |
np.indices((m,n)) | 创建一个m行n列的矩阵 |
np.random.rand(m,n) | 创建一个m行n列的随机数组 |
np.ones((m,n),dtype) | 创建一个m行n列全1的数组,dtype是数据类型 |
np.empty((m,n),dtype) | 创建一个m行n列全0的数组,dtype是数据类型 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import numpy as np a1 = np.array([ 1 , 2 , 3 , 4 , 5 , 6 ]) a2 = np.arange( 1 , 10 , 3 ) a3 = np.linspace( 1 , 10 , 3 ) a4 = np.indices(( 3 , 4 )) a5 = np.random.rand( 3 , 4 ) a6 = np.ones(( 3 , 4 ), int ) a7 = np.empty(( 3 , 4 ), int ) print (a1) print ( "===========================================================" ) print (a2) print ( "===========================================================" ) print (a3) print ( "===========================================================" ) print (a4) print ( "===========================================================" ) print (a5) print ( "===========================================================" ) print (a6) print ( "===========================================================" ) print (a7) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = [ 1 2 3 4 5 6 ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = [ 1 4 7 ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = [ 1. 5.5 10. ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = [[[ 0 0 0 0 ] [ 1 1 1 1 ] [ 2 2 2 2 ]] [[ 0 1 2 3 ] [ 0 1 2 3 ] [ 0 1 2 3 ]]] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = [[ 0.00948155 0.7145306 0.50490391 0.69827703 ] [ 0.18164292 0.78440752 0.75091258 0.31184394 ] [ 0.17199081 0.3789 0.69886588 0.0476422 ]] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = [[ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ]] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = [[ 0 0 0 0 ] [ 0 0 0 0 ] [ 0 0 0 0 ]] |
在建立一个简单的数组后,可以查看数组的属性
属性 | 描述 |
ndarray.ndim | 数组轴的个数,也被称为秩 |
ndarray.shape | 数组在每个维度上大小的整数元组 |
ndarray.size | 数组元素的总个数 |
ndarray.dtype | 数组元素的数据类型,dtype类型可以用于创建数组 |
ndarray.itemsize | 数组中每个元素的字节大小 |
ndarray.data | 包含实际数组元素的缓冲区地址 |
ndarray.flat | 数组元素的迭代器 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import numpy as np a6 = np.ones(( 3 , 4 ), int ) print (a6) print ( "=========================================" ) print (a6.ndim) print ( "=========================================" ) print (a6.shape) print ( "=========================================" ) print (a6.size) print ( "=========================================" ) print (a6.dtype) print ( "=========================================" ) print (a6.itemsize) print ( "=========================================" ) print (a6.data) print ( "=========================================" ) print (a6.flat) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = [[ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ]] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ( 3 , 4 ) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 12 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = int32 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 4 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = <memory at 0x0000020D79545908 > = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = <numpy.flatiter object at 0x0000020D103B1180 > |
数组在numpy中被当做对象,可以采用< a >.< b >()方式调用一些方法。
ndarray类的形态操作方法
方法 | 描述 |
ndarray.reshape(n,m) | 不改变数组ndarray,返回一个维度为(n,m)的数组 |
ndarray.resize(new_shape) | 与reshape()作用相同,直接修改数组ndarray |
ndarray.swapaxes(ax1,ax2) | 将数组n个维度中任意两个维度进行调换 |
ndarray.flatten() | 对数组进行降维,返回一个折叠后的一维数组 |
ndarray.ravel() | 作用同np.flatten(),但返回的是一个视图 |
ndarray类的索引和切片方法
方法 | 描述 |
x[i] | 索引第i个元素 |
x[-i] | 从后向前索引第i个元素 |
x[n:m] | 默认步长为1,从前向后索引,不包含m |
x[-m:-n] | 默认步长为1,从前向后索引,结束位置为n |
x[n: m :i] | 指定i步长的由n到m的索引 |
除了ndarray类型方法外,numpy库提供了一匹运算函数
函数 | 描述 |
np.add(x1,x2[,y]) | y = x1 + x2 |
np.subtract(x1,x2[,y]) | y = x1 -x2 |
np.multiply(x1,x2[,y]) | y = x1 * x2 |
np.divide(x1,x2[,y]) | y = x1 /x2 |
np floor_divide(x1,x2[,y]) | y = x1 // x2 |
np.negative(x[,y]) | y = -x |
np.power(x1,x2[,y]) | y = x1 ** x2 |
np.remainder(x1,x2[,y]) | y = x1 % x2 |
numpy库的比较运算函数
函数 | 符号描述 |
np.equal(x1,x2[,y]) | y = x1 == x2 |
np.not_equal(x1,x2[,y]) | y = x1 != x2 |
np.less(x1,x2,[,y]) | y = x1 < x2 |
np.less_equal(x1,x2,[,y]) | y = x1 < = x2 |
np.greater(x1,x2,[,y]) | y = x1 > x2 |
np.greater_equal(x1,x2,[,y]) | y >= x1 >= x2 |
np.where(condition[x,y]) | 根据条件判断是输出x还是y |
numpy库的其他运算函数
函数 | 描述 |
np.abs(x) | 计算济源元素的整形、浮点、或复数的绝对值 |
np.sqrt(x) | 计算每个元素的平方根 |
np.squre(x) | 计算每个元素的平方 |
np.sign(x) | 计算每个元素的符号1(+),0,-1(-) |
np.ceil(x) | 计算大于或等于每个元素的最小值 |
np.floor(x) | 计算小于或等于每个元素的最大值 |
np.rint(x[,out]) | 圆整,取每个元素为最近的整数,保留数据类型 |
np.exp(x[,out]) | 计算每个元素的指数值 |
np.log(x),np.log10(x),np.log2(x) | 计算自然对数(e),基于10,,2的对数,log(1+x) |
到此这篇关于Python基础之numpy库的使用的文章就介绍到这了,更多相关Python numpy库的使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_55016379/article/details/116198293