第五节 numpy的简单使用

import numpy as np
import random

# 使用numpy生成数组的三种方式

t1 = np.array([1,2,3])
print(t1)

t2 = np.array(range(10))
print(t2)

t3 = np.arange(0,10,2)
print(t3)

# 查看数据里的数据类型
print(t3.dtype)

# 创建的时候指定数据类型
t4 = np.array(range(10), dtype="int8")
print(t4.dtype)

t5 = np.array([1, 1, 0, 1, 0, 1], dtype="bool")
print(t5)
print(t5.dtype)

# 对已存在数组改变数据类型
t6 = t5.astype("int8")
print(t6)
print(t4.dtype)

# numpy中的小数
t7 = np.array([random.random() for i in range(10)])
# 对t7取两位小数
t8 = np.round(t7, 2)
print(t8)
原文地址:https://www.cnblogs.com/kogmaw/p/12559077.html