# Ch0: numpy ###### tags: `AI` `Python` ## 註記 此部份節錄自章節1-3 Numpy相關概念對於TensorFlow可是很重要的 可以的話也回去翻一下矩陣運算以及線性代數的書 ## Sample Code ```python= import numpy as np print("create an array with all valued zero, sized 3, 5, 2\n") a = np.zeros((3, 5, 2)) # Create an array, sized 3, 5, 2, valued all zero print("a : ") print(a) # print x print("dimension of a : ") print(a.ndim) # print dimensions of a print("shape of a : ") print(a.shape) # print shape of a print("data type of a : ") print(a.dtype) # print datatype of data of a print("\n\n") print("create an array, [[1, 2], [3, 4]] in this content") print("b : ") b = np.array([[1, 2], [3, 4]]) print(b) print("\n\n") print("create an array with all valued zero , sized 2, data type uint8") c = np.zeros(2, dtype='uint8') print("c : ") print(c) print(c.dtype) print("\n\n") print("create an array with all valued one, sized 2, 3") print("d : ") d = np.ones((2, 3)) # Create an array, sized 2, 3, valued all one print(d) print("\n\n") print("create an array with all valued seven, sized 2, 3") print("e : ") e = np.full((2, 3), 7) # Create an array, sized 2, 3, valued all seven print(e) print("\n\n") print("create an array, value from 0 to 4 (less than 5)") print("f : ") f = np.arange(5) # Create an array, value from 0-4 print(f) print("\n\n") print("copy array, or create an array sized same as other array") print("g : ") g = np.copy(f) # g copied array f print(g) print("\n\n") print("create an array, shaped same as f, value all zero") h = np.zeros(f.shape) print("h : ") print(h) print("\n\n") print("create an array, shaped same as f, value all one") i = np.ones(f.shape) print("i : ") print(i) print("\n\n") print("get part of array") j = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("j : ") print(j) print(j[1, 2]) print(j[1:3, 1:3]) print("\n\n") print("reshape array") k = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) l = np.reshape(k, (2, 4)) print("k : ") print(k) print("l : ") print(l) print("\n\n") print("transpose array") m = np.array([[1, 2, 3], [4, 5, 6]]) print("m : ") print(m) print("transpose array from m : ") print(np.transpose(m)) print("\n\n") print("array calculating") print("np.array([[1, 2], [3, 4]])+np.array([[1, 2], [3, 4]]) : ") print(np.array([[1, 2], [3, 4]])+np.array([[1, 2], [3, 4]])) print("np.array([1, 2])*np.array([3, 4]) : ") print(np.array([1, 2])*np.array([3, 4])) print("\n\n") print("array index") n = np.array([0, 1, 2, 3, 4]) print(n) print("set index[1, 3]") idx = [1, 3] n[idx] = 7 print("after giving this index value 7, n : ") print(n) n[idx] = (8, 9) print("after giving this index value 8, 9, n : ") print(n) o = n[idx] print("after giving value to array o, array o : ") print(o) print("print o[[1, 1, 0, 1]]") print(o[[1, 1, 0, 1]]) p = np.array([0, 1, 2]) print(p) IDX = np.array([True, False, True]) print("after giving it index : ") print(p[IDX]) p[IDX] = 7 print(p) print("\n\n") print("max, min, sum, mean etc... at specific axis of array") q = np.array([[1, 2], [3, 4]]) print("np.max(q) : ") print(np.max(q)) print("np.max(q, axis=0)") print(np.min(q, axis=0)) print("np.min(q, axis=1)") print(np.max(q, axis=1)) print("np.sum(q, axis=0)") print(np.sum(q, axis=0)) print("np.mean(q, axis=0)") print(np.mean(q, axis=0)) ``` ## source * tf.Keras 深度學習攻略手冊 * 精通Python