Python数据分析 3.numpy基础库
1.数组的创建
import numpy as np
t1 = np.array([1,2,3,])
print(t1)
print(type(t1))
t2 = np.array(range(3))
print(t2)
t3 = np.arange(4,10,2)
print(t3)
print(t3.dtype)
[1 2 3]
<class 'numpy.ndarray'>
[0 1 2]
[4 6 8]
int32
numpy中常见的数据类型:
#numpy中的数据类型
t4 = np.array(range(1,4),dtype="i1")
print(t4)
print(t4.dtype)
#numpy中的bool类型
t5 = np.array([1,1,0,1,0,0],dtype=bool)
print(t5)
print(t5.dtype)
#调整数据类型
t6 = t5.astype("int8")
print(t6)
print(t6.dtype)
#numpy中的小数
t7 = np.array([random.random() for i in range(3)])
print(t7)
print(t7.dtype)
#numpy小数点后几位
t8 = np.round(t7,2)
print(t8)
print("%.2f"%random.random())
[1 2 3]
int8
[True True False True False False]
bool
[1 1 0 1 0 0]
int8
[0.50897424 0.78931079 0.69345471]
float64
[0.51 0.79 0.69]
0.78
数组的形状:
import numpy as np
t1 = np.arange(12)
print(t1)
print(t1.shape)
t2 = np.array([[1,2,3],[4,5,6]])
print(t2)
print(t2.shape)
# 三维数组
t3 = np.array([[[