当前位置:网站首页>【NumPy中数组创建】
【NumPy中数组创建】
2022-07-26 00:24:00 【In my opinion】
目录
二、利用array创建数组
三、利用arange创建数组
四、随机数创建数组
五、ndarray对象
六、其他方式创建数组
七、数组的切片与索引
一、NumPy是什么?
1.NumPy是科学计算基础库,提供大量科学计算相关功能,如数据统计,随机数生成,其提供最核心类型为多维数组(ndarray),支持大量的维度数组与矩阵运算,支持向量处理ndarray对象,提高程序运算速度。
2.NumPy安装
pip install numpy
二、利用array创建数组
1.numpy模块中的array函数可生成多维数组,若生成一个二维数组,需要向array函数传递一个列表类型参数,每一个列表元素是一维ndarray类型数组,作为二维数组的行,numpy中shape[n]属性,可以获得每一维元素个数,n从0开始
#导入模块
import numpy as np
#使用array函数创建一维数组
a=np.array[1,2,3,4]
print(a)
#使用array函数创建二维数组
b=np.array([[1,2,3],[4,5,6],[7,8,9]])
print(b)
#使用array函数创建三维数组
c=np.array([[[1,2,3],[4,5,6],[7,8,9]]])
print(c)
#array函数中dtype使用,指定元素的类型
d=np.array([1,2,3],dtype=float)
print(d)
#array中ndmin使用,指定数组的维度
m=np.array([1,2,3],dtype=float,ndmin=3)
print(m)三、利用arange创建数组
使用arange函数创建数值范围并返回ndarray对象,格式:
numpy.arange(start,end,step)
| 参数 | 说明 |
| start | 起始值(默认为0) |
| end | 终止值(不包含) |
| step | 步长,默认为1 |
| dtype | 返回ndarray的数据类型 |
#导入numpy
import numpy as np
#复习range的使用(start,end,step) [start,end)
a=list(range(1,10)) #步长是1
print(a)
b=list(range(10))
print(b)
c=list(range(1,10,3))
print(c)
#arange创建数组(start,end,step)
m=np.arange(1,11)
print(m)
#设置步长
n=np,arange(1,11,2)
print(n)
#设置dtype
e=np.arange(10,20,2,dtype=float)
print(e)[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 4, 7]
[1 2 3 4 5 6 7 8 9 10][1 3 5 7 9]
[10. 12. 14. 16. 18.]
四、随机数创建数组
numpy.random.random(size=None)
该方法返回[0.0~1.0)之间的随机浮点数
#导入模块
import numpy as np
#使用random创建一维数组
a=np.random.random(size=10)
#创建二维数组
b=np.random.random(size=(2,3)) #2行3列的数组
#创建三维的数组
c=np.random.random(size=(2,3,4)) #两个3行4列创建随机整数方法:
numpy.random.randint(low,high,size)
该方法返回随机整数,如果只有low,返回范围是[0,low),如果有high,返回范围为[low,high)
#导入模块
import numpy as np
#创建0~10一维随机整数
a=np.random.randint(11,size=10)
#创建0~10二维随机整数
b=np.random.randint(11,size=(2,3)) #2行3列
#创建5~10三维随机整数
c=np.random.ramdint(5,11,size=(2,3,4)) #2个3行4列创建标准正态分布(期望为0,方差为1):
numpy.random.randn(d0,d1...dn):dn表示每个维度
#导入模块
import numpy as np
#创建一维
a=np.random.randn(3)
#创建二维
b=np.random.randn(2,3)
#创建三维
c=np.random.randn(2,3,4)创建指定期望和方差的正态分布:
numpy.random.normal(loc,scale,size)
loc:期望,scale:方差
五、ndarray对象
1.ndarray对象,是一系列同类型数据的集合,以下标0开始进行集合中元素的索引
2.ndarray的对象属性
| 属性 | 说明 |
| .ndim | 秩,轴的数量或维度数量 |
| .shape | 数组的维度,对于矩阵(n,m) |
| .size | 数组元素总个数 |
| .dtype | 元素类型 |
| .itemsize | 每个元素大小,字节为单位 |
| .flags | 对象的内存信息 |
| .real | ndarray元素的实部 |
| .imag | ndarray元素的虚部 |
六、其他方式创建数组
1.zeros创建,元素用0.0填充:
numpy.zeros((5,),dtype=int) #5个0元素的数组
numpy.zeros((2,3)) #2行3列0.0元素数组
2.ones创建,元素用1.0填充
3.empty创建指定形状、数据类型未初始化的数组,元素的值是之前内存的值
4.linspace创建等差数列
numpy.linspace(5,20,5,endpoint=False) #不包括20
[5. 8. 11. 14. 17.]
5.logspace创建等比数列
numpy.logspace(start,stop,num,endpoint,base,dtype)
其中,序列起始值base**start,终止值base**stop,生成num个
七、数组的切片与索引
一维数组:
1.索引:x=numpy.arange(10)
正向索引:x[0]~x[9]
反向索引:x[-9]~x[-1]
2.切片:[start,end,step]
切片中正向操作:
x[:] :从开始到结尾
x[start:] : 从start开始到结尾
x[:end] :从头开始到end-1
x[start:end] :从start开始到end-1
x[start:end:step] :从start开始到end-1,步长为step
切片中负向操作:
x[::-1]:反向输出
x[-5:-2]:倒数第5个~倒数第3个
二维数组:
1.索引:
x=numpy.arange(1,13)
a=x.reshape((4,3)
#索引第二行第三列
print(a[1,2]) 或者print(a[1][2])
#获取第二行第三列和第三行第一列
print(a[1,2],a[2,0])或者print(a[(1,2),(2,0)]
2.切片:[行进行切片,列进行切片] [start:end:step,start:end:step]
x=numpy.arange(1,13)
a=x.reshape((4,3)
print(a(:,:)) #获取所有行列
print(a[:,1])#所有行第二列
print(a[1,:]) #所有列第二行
print(a[::2,:]) #所有列奇数行
边栏推荐
- 2022/7/19 考试总结
- Markdown writing platform
- 【redis】③ 数据淘汰策略、pipeline 管道命令、发布订阅
- Use of redis
- FreeRTOS personal notes - semaphore
- Study on bovine serum protein modified phenolic acids and alkaloids small molecules / coupled microspheres protein / bovine erythrocyte SOD
- Tid-mop: a comprehensive framework for security management and control under the scenario of data exchange
- Opencv learning Day6
- 2022/7/25 考试总结
- 计算物理期刊修改
猜你喜欢

FreeMarker view integration

NVIDIA programmable reasoning accelerator tensorrt learning notes (III) -- Accelerating reasoning

FreeRTOS personal notes - mutex

sql(基础二)

MWEC:一种基于多语义词向量的中文新词发现方法

Semaphore

快速入门顺序表链表

【论文笔记】—目标姿态估计—EPro-PnP—2022-CVPR

【Redis】② Redis通用命令;Redis 为什么这么快?;Redis 的数据类型

Opencv learning Day6
随机推荐
Research progress of data traceability based on the perspective of data element circulation
NVIDIA programmable reasoning accelerator tensorrt learning notes (III) -- Accelerating reasoning
软件测试同行评审到底是什么?
@The difference between requestparam and @pathvariable annotations
Wechat applet for loop
Elementary C language - branch statements (if, switch)
MPLS实验
基于SEIR模型的网络医疗众筹传播建模与仿真分析
Verilog语法基础HDL Bits训练 05
OPENCV学习DAY6
【Redis】② Redis通用命令;Redis 为什么这么快?;Redis 的数据类型
[contents] mqtt, nodejs projects
Revision of Journal of Computational Physics
FreeRTOS personal notes - mutex
JSON data development
Research on visualization method of technology topic map based on clustering information
MPLS experiment
GOM和GEE引擎黑屏不显示界面,装备地图怪物的解决方法
Modeling and simulation analysis of online medical crowdfunding communication based on SEIR model
Hefei approved in advance