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')