#np.random.randn 生成隨機數組,每運行一次會改變一次
arr=np.random.randn(9)
print(arr)
[ 0.2303069 1.00300936 -0.92357035 0.06525841 0.19994871 -1.40363692
0.01230236 -0.08950691 -1.13518494]
#最值 min max
print(arr.min())
print(arr.max())
-1.403636921406203
1.003009355191414
#均值、求和、排序
print(arr.mean())
print(arr.sum())
arr.sort()
print(arr)
-0.22678593093886062
-2.0410733784497457
[-1.40363692 -1.13518494 -0.92357035 -0.08950691 0.01230236 0.06525841
0.19994871 0.2303069 1.00300936]
# 創建一個二維數組
arr1=np.random.randn(5,3)
print(arr1)
[[-0.37967759 1.35964448 -0.91838218]
[ 0.59445228 0.50795044 -1.04871713]
[ 0.95627647 0.51861237 -1.91861238]
[ 0.71488425 0.80140304 0.85057234]
[-0.83144865 1.40915456 -0.48493178]]
# 求最值,均值,排序
print(arr1.min())
print(arr1.mean())
arr1.sort
print(arr1)
-1.9186123788695604
0.14207870043754137
[[-0.37967759 1.35964448 -0.91838218]
[ 0.59445228 0.50795044 -1.04871713]
[ 0.95627647 0.51861237 -1.91861238]
[ 0.71488425 0.80140304 0.85057234]
[-0.83144865 1.40915456 -0.48493178]]
#linspace函數:區間等分
np.linspace(0,10,2)
array([ 0., 10.])
np.linspace(0,10,5)
array([ 0. , 2.5, 5. , 7.5, 10. ])
# 生成0-10之間均勻分布的11個數,包括0,10
np.linspace(0,10,11)
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
#(1)矩陣乘法 dot
x=np.array([[1,2,3],[4,5,6]])
print(x)
[[1 2 3]
[4 5 6]]
y=np.array([[6,23],[-1,7],[8,9]])
print(y)
[[ 6 23]
[-1 7]
[ 8 9]]
x.dot(y) #矩陣的乘法,注意:矩陣左邊的行數=右側矩陣的列數
array([[ 28, 64],
[ 67, 181]])
#(2)矩陣求逆 inv
from numpy.linalg import inv
m=np.array([[4,2],[3,1]])
print(m)
[[4 2]
[3 1]]
inv(m)
array([[-0.5, 1. ],
[ 1.5, -2. ]])
from numpy import random
# normal方法:生成符合正態分布的隨機數
rArray=random.normal(size=(4,4))
print(rArray)
[[-0.06795971 -0.9353821 2.26863073 -0.75476106]
[-0.57931197 -0.35099822 0.84149375 1.55841483]
[ 1.3666115 0.16391788 -0.64410767 -1.5884498 ]
[-0.86659227 -0.60654638 1.34484013 -0.2681763 ]]
# randint方法:產生給定上下限范圍內的隨機選取整數
random.randint(4,10)
9
i=0
while i <20:
print(random.randint(0,2)) #隨機生成0和1,每次運行都會隨機生成
i+=1
0
0
0
1
1
1
0
0
0
0
1
0
1
0
0
1
1
0
1
0