numpy科学计算器库入门4 随机数生成及矩阵的运算

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zy_505775013/article/details/88669778
import numpy as np
sample1 = np.random.random((3,2))#生成3行2列的从0到1的随机数
print(sample1)

[[0.02989707 0.17760986]
 [0.15221831 0.02596271]
 [0.11625275 0.2737317 ]]

sample2 = np.random.normal(size=(3,2))#生成3行2列符合正态分布的随机数
print(sample2)

[[ 0.77482094  1.21301245]
 [-0.36530729 -0.00649329]
 [-2.30899806  1.16538775]]

sample3 = np.random.randint(0,10,size=(3,2))#生成3行2列取值0到10 的随机整数
print(sample3)

[[4 8]
 [0 1]
 [3 8]]

np.sum(sample1)#对所有的元素进行求和

0.7756723971835694

np.min(sample1)#从所有元素中找到最小的一个

0.025962713395063397

np.max(sample1)#从所有元素中找到最大的一个

0.2737316980011486

np.sum(sample1,axis=0) #对每一列求和

array([0.29836812, 0.47730428])

np.sum(sample1,axis=1)#对每一行求和

array([0.20750693, 0.17818102, 0.38998445])

np.argmin(sample1)#找到最小值的索引

3

np.argmax(sample1)#找到最大值的索引

5

print(np.mean(sample1))#求平均值
print(sample1.mean())#求平均值

0.12927873286392824
0.12927873286392824

np.median(sample1)#求中位数

np.sqrt(sample1)#求开平方的结果

array([[0.17290768, 0.42143785],
       [0.39015165, 0.16112949],
       [0.34095857, 0.52319375]])

sample4 = np.random.randint(0,10,size=(1,10))
print(sample4)

[[8 6 9 5 3 5 7 0 1 5]]

np.sort(sample4)#排序

array([[0, 1, 3, 5, 5, 5, 6, 7, 8, 9]])

np.clip(sample4,2,7)#sample4中的元素小于的变成2 大于7的变成7

array([[7, 6, 7, 5, 3, 5, 7, 2, 2, 5]])









猜你喜欢

转载自blog.csdn.net/zy_505775013/article/details/88669778
今日推荐