栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

python3-numpy基础--数据类型、创建numpy数组

python3-numpy基础--数据类型、创建numpy数组

1、为什么学习numpy?
  • 快速
  • 方便
  • 有科学计算的基础库
2、什么是numpy?

NumPy(Numerical Python)是 Python 科学计算的基础包,它是一个开源的 Python 扩展库,用来支持大数据量的高维数组和矩阵运算,比 Python 自身的嵌套列表(该结构也可以用来表示矩阵)结构要高效的多。

3、numpy的数据类型


4、numpy数组的简单使用
import random
import numpy as np

# 使用numpy 生成数组,得到ndarray 的数据类型
t1 = np.array([1,2,3])
print(t1)  # [1 2 3]
print(type(t1))  # 

t2 = np.array(range(10))
print(t2)  # [0 1 2 3 4 5 6 7 8 9]
print(type(t2))  # 

t3 = np.arange(10)
print(t3)  # [0 1 2 3 4 5 6 7 8 9]
print(type(t3))  # 

t4 = np.arange(2, 10, 2)
print(t4)  # [2 4 6 8]
print(type(t4))  # 

# 查看数据类型
print(t1.dtype)  # int32
print(t2.dtype)  # int32
print(t3.dtype)  # int32
print(t4.dtype)  # int32

# 创建numpy 时指定 数据类型
t5 = np.array(range(1,4),dtype="int8")
print(t5)  # [1 2 3]
print(t5.dtype)  # int8

t5 = np.array(range(1,4),dtype=float)
print(t5)  # [1. 2. 3.]
print(t5.dtype)  # float64

t5 = np.array(range(1,4),dtype=int)
print(t5)  # [1 2 3]
print(t5.dtype)  # int32

t5 = np.array([1,1,0,1,0],dtype=bool)
print(t5)  # [ True  True False  True False]
print(t5.dtype)  # bool  布尔类型

# 修改数据类型
t6 = t5.astype("int8")
print(t6)  # [1 1 0 1 0]
print(t6.dtype)  # int8

# numpy 中的小数
t7 = np.array([random.random() for i in range(3)])
print(t7)  # [0.94210459 0.37335804 0.38199125]
print(t7.dtype)  # float64

# 保留小数
t8 = np.round(t7,2)
print(t8)
5、numpy数组的形状

https://www.bilibili.com/video/BV1hx411d7jb?p=15

https://www.bilibili.com/video/BV1hx411d7jb?p=14
https://www.bilibili.com/video/BV1hx411d7jb?p=15
https://www.runoob.com/numpy/numpy-tutorial.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/439177.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号