import numpy as np
a = np.zeros((2,2)) #建立2x2的全零矩陣
print(a)
b = np.ones((1,2)) #建立1x2的全一矩陣
print(b)
c = np.full((2,2), 7) #建立2x2的全為7的矩陣
print(c)
d = np.eye(2) #建立全1的2x2對角矩陣
print(d)
e = np.random.random((2,2)) #建立2x2的隨機數矩陣
print(e)
[[0. 0.]
[0. 0.]]
[[1. 1.]]
[[7 7]
[7 7]]
[[1. 0.]
[0. 1.]]
[[0.57761083 0.61231605]
[0.10407747 0.88935241]]
切片
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
b = a[:2, 1:3] #獲取前兩行,第二列第三列的資料
[[2 3]
[6 7]]
a = np.array([[1,2], [3, 4], [5, 6]])
print(a[[0, 1, 2], [0, 1, 0]])
[1 4 5]
布林陣列索引
import numpy as np
a = np.array([[1,2], [3, 4], [5, 6]])
bool_idx = (a > 2)
print(bool_idx)
print(a[bool_idx]) # prints "[3 4 5 6]"
print(a[a > 2]) # prints "[3 4 5 6]"
import numpy as np
x = np.array([1, 2])
print(x.dtype) # prints "int64"
x = np.array([1.0, 2.0])
print(x.dtype) # prints "float64"
x = np.array([1, 2], dtype=np.int64)
print(x.dtype) # prints "int64"
矩陣加減乘除直接使用運算符號即可。如a*b
import numpy as np
x = np.array([[1,2],[3,4]])
print(np.sum(x)) # prints "10"
print(np.sum(x, axis=0)) # prints "[4 6]"
print(np.sum(x, axis=1)) # prints "[3 7]"
print(x.t) # prints "[[1 3]
# [2 4]]"
print(x.dot(v))
print(np.dot(x, v)) #向量內積
import numpy as np
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x) # 建立乙個維度與x一樣的空矩陣
# x的每一行都加上v向量
for i in range(4):
y[i, :] = x[i, :] + v
print(y)
# [[ 2 2 4]
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]
import numpy as np
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1)) # 堆疊行4次
print(vv) # prints "[[1 0 1]
# [1 0 1]
# [1 0 1]
# [1 0 1]]"
y = x + vv
print(y) # prints "[[ 2 2 4
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]"
#當前版本可直接加
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = x + v
print(y) # prints "[[ 2 2 4]
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]"
正則知識點補充
1.與正規表示式有關的字串物件的方法 string.replace pattern,string 替換在正規表示式查詢中找到的文字。string.search pattern 通過正規表示式查詢相應的字串,只是判斷有無匹配的字串。如果查詢成功,search 返回匹配串的位置,否則返回 1。strin...
property知識點補充
1 synthesize和 dynamic作用 1 property有兩個對應的詞,乙個是 synthesize,乙個是 dynamic。如果 synthesize和 dynamic都沒寫,那麼預設的就是 syntheszie var var 2 synthesize的語義是如果你沒有手動實現set...
DOM知識點補充
一 元素物件的屬性及自定義屬性的設定或獲取?物件.屬性 物件 屬性 物件.getattribute 屬性名 物件.setattribute 屬性名 屬性值 物件.removeattribute 屬性名 二 outerhtml 獲取當前元素物件及所有內容 innerhtml 設定或獲取當前元素物件內的...