一、Numpy
Python中做科学计算的基础库,重在数值计算,多用于处理大型多维数组上的数值运算。
特点:快速、方便、科学计算的基础库
安装:pip install numpy
二、numpy创建数组(矩阵)
import numpy as np
def nu():
a = np.array([1,2,3,4,5])
print (a)
b = np.array(range(0,6))
print (b)
print("class: %s " % str(type(a)))
print("type: %s " % a.dtype)
return None
if __name__ == "__main__":
nu()
执行结果:
F:\Python\3.7\python.exe E:/PythonProject/Scripts/data_analysis.py
[1 2 3 4 5]
[0 1 2 3 4 5]
class:
type: int32
三、Numpy中更多数据类型
d = np.array(range(0,10), dtype="float")
print(d)
print("type: %s " % d.dtype)
t = np.array([1,0,2,3,0], dtype=bool)
print(t)
print("type: %s " % t.dtype)
e =d.astype('i1')
print(e)
print("type: %s " % e.dtype)
f = round(random.random(),2)
print(f)
结果:
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
type: float64
[ True False True True False]
type: bool
[0 1 2 3 4 5 6 7 8 9]
type: int8
0.75
四、数组的形状
import numpy as np
t = np.arange(12)
print (t)
print (t.shape)
t1 = np.array([[1,2,3,4],[5,6,7,8]])
print (t1.shape)
print(t1.reshape(4,2))
print(t1.shape)
print(t1.reshape(4, 2).shape)
执行结果:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
(12,)
(2, 4)
[[1 2]
[3 4]
[5 6]
[7 8]]
(2, 4)
(4, 2)
五、数组和数,数组的计算