import numpy as np
import random
t1 = np.array([0, 1, 2, 3, 4])
print(t1,'\n')
t2 = np.array(range(5))
print(t2,'\n')
t3 = np.arange(5)
print(t3,'\n')
print(type(t3),'\n')
print(t3.dtype,'\n')
"""创建不同类型的数组,用dtype参数"""
t4 = np.array(range(10), dtype=float)
print(t4)
print(t4.dtype,'\n')
t5 = np.array([1, 0, 0, 1, 0, 1], dtype=bool)
print(t5)
print(t5.dtype,'\n')
"""数据的类型转换,用astype("要转换的类型")方法"""
t6 = t5.astype("int8")
print(t6, t6.dtype,'\n')
"""修改小数的位数,用roung方法"""
t7 = np.array([random.random() for i in range(10)])
print(t7, t7.dtype,'\n')
t8 = np.round(t7,3)
print(t8, t8.dtype,'\n')
print(round(random.random(),3),'\n')
"""数组的形状"""
d1 = np.array(range(12))
print(d1,d1.shape,'\n')
d2 = np.array([[1, 2, 3], [4, 5, 6]])
print(d2,d2.shape,'\n')
"""修改数组的形状 reshape()"""
l = np.array([0,1,2,3,4,5,6,7,8,9,10,11])
d3 = l.reshape(3, 4)
print(d3,d3.shape,'\n')
d4 = np.array(range(24)).reshape(2,3,4)
print(d4, d4.shape,'\n')
d5 = np.array(range(48)).reshape(2,2,3,4)
print(d5,d5.shape,'\n')
"""将多维数组展开,flatten()"""
d6 = d5.reshape(d5.shape[0]*d5.shape[1]*d5.shape[2]*d5.shape[3],)
print(d6, d6.shape,'\n')
d7 = d5.flatten()
print(d7, d7.shape,'\n')
"""数组与数字,数组与数组进行计算"""
a1 = np.array(range(12)).reshape(3,4)
print('数组与数字:\n',a1,'\n')
print(a1+5,'\n')
print(a1-5,'\n')
print(a1*5,'\n')
print(a1/5,'\n')
print('数组与数组:\n',a1+a1,'\n')
print(a1-a1,'\n')
print(a1*a1,'\n')
print(a1/a1,'\n')
"""数据的索引与切片"""
import numpy as np
t1 = np.array(range(24)).reshape((4, 6))
print(t1, '\n')
print(t1[1:], '\n')
print(t1[[0, 2, 3]], '\n')
print(t1[:, 0], '\n')
print(t1[:, [2, 4, 5]], '\n')
print(t1[[0,2,3],[1,4,5]], '\n')
"""numpy中数值的修改"""
print(t1, '\n')
print(t1[t1<10], '\n')
t1[t1<10] = 3
print(t1, '\n')
"""numpy中的三元运算符where"""
t2 = np.where(t1 < 10, 0, 20, '\n')
print(t2)
"""numpy中的裁剪clip"""
t3 = t1.clip(10,18)
print(t3, '\n')