生成array
#一维
x = np.array([1,2,3])
#二维
y = np.array([[1,2],[3,4],[5,6]])
# Print the first column of y
y[:,0]
# Print the second row of y
y[1]
# Print the last element of y
#y[2,1] #OR
y[-1,-1]
a = np.arange(1,11)
print(a)
[ 1 2 3 4 5 6 7 8 9 10]
# range函数的生成区间左闭右开
注意:所有的index都是从0开始计算的,不能与矩阵的索引习惯混淆
重要函数
生成随机数
# 生成5个0到1的随机数
np.random.rand(5)
生成随机整数
np.random.seed(1234)
np.random.randint(0,101,size(3,3))
array([[47, 83, 38],
[53, 76, 24],
[15, 49, 23]])
.copy
a = np.array([1,2,3])
b = a.copy()
.repeat
# complete the following to repeat `a` twice, row-wise
print(np.repeat(a,2,axis=0))
[[2 4 2]
[2 4 2]
[1 3 2]
[1 3 2]]
# now column-wise
print(np.repeat(a,2,axis=1))
[[2 2 4 4 2 2]
[1 1 3 3 2 2]]
.reshape
矩阵操作
print(np.add(A,B))
print(np.subtract(A,B))
print(np.multiply(A,B))
print(np.divide(A,B))
np.inner(a,b)
np.dot(b,a)
np.matmul(b,a)
Comments | NOTHING