numpy简单例题

#1、创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1
import numpy as np
s1=np.zeros(shape=10)
s1[4]=1
s1

array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])

#2、创建一个元素为从10到49的ndarray对象

np.random.randint(10,50,size=10)

array([10, 35, 33, 19, 34, 33, 29, 42, 19, 22])

np.linspace(10,49,10)  #10到49均分成10份

array([10. , 14.33333333, 18.66666667, 23. , 27.33333333,
31.66666667, 36. , 40.33333333, 44.66666667, 49. ])

a=np.arange(10,50)
a

array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49])

#3、将第二题元素位置反转
a[::-1]

array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10])

#4、使用np.random.random创建一个10*10的ndarray对象,并打印出最大最小元素
a4=np.random.random(size=(10,10))
a4

array([[0.5622766 , 0.11024025, 0.54759318, 0.07086502, 0.30194698,
0.94127056, 0.90620362, 0.24142539, 0.84479059, 0.79890956],
[0.09417458, 0.43564112, 0.31865056, 0.64912766, 0.71106889,
0.78363619, 0.77593867, 0.37541201, 0.69828262, 0.13911184],
[0.09806677, 0.79576384, 0.52297016, 0.88634979, 0.25990919,
0.7490997 , 0.13730559, 0.90144644, 0.97227104, 0.19702788],
[0.31992537, 0.55152616, 0.63847898, 0.92698579, 0.01846235,
0.27698572, 0.10205816, 0.92509311, 0.53757403, 0.5561305 ],
[0.77167545, 0.18553457, 0.41577397, 0.57109938, 0.4990056 ,
0.81669034, 0.39300295, 0.16677413, 0.86708894, 0.70152298],
[0.03381229, 0.38423466, 0.60926444, 0.34723288, 0.79453574,
0.51054134, 0.81158369, 0.78549877, 0.44253459, 0.27374706],
[0.8120598 , 0.59104821, 0.01256257, 0.93459978, 0.23386372,
0.42754771, 0.31350232, 0.57671285, 0.02523678, 0.17352996],
[0.16218939, 0.33930872, 0.02135162, 0.36779925, 0.0644362 ,
0.45780419, 0.06912587, 0.36954142, 0.42229335, 0.93250317],
[0.69128546, 0.51602169, 0.91709331, 0.02406427, 0.72474664,
0.70480915, 0.16368609, 0.47724194, 0.74440481, 0.612721 ],
[0.77237246, 0.51976944, 0.77832542, 0.65741988, 0.48525145,
0.51741086, 0.02865275, 0.85205137, 0.64890416, 0.18271778]])

zmin,zmax=a4.min(),a4.max()
zmin,zmax

(0.012562573199310756, 0.9722710364773866)

#5、创建一个10*10的ndarray对象,且矩阵边界全为1,里面全为0
nd = np.zeros(shape = (10,10),dtype = np.int8)
nd[[0,9]] = 1     #第1行,第10行
nd[:,[0,9]] = 1    # 第一列,第十列
nd

array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)

a5 = np.ones((10,10))
a5[1:-1,1:-1]=0
a5

array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])

#6、创建一个每一行都是从0到4的5*5矩阵
l = [0,1,2,3,4]
nd = np.array(l*5)
nd.reshape(5,5)

array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])

nd6_1=np.arange(0,5,1)  #等差数列,差值为1
nd6_1

array([0, 1, 2, 3, 4])

nd6=np.arange(0,25).reshape(5,5)
nd6[0:5]=nd6_1  #第0行到第5行,赋值是赋的nd6_1的值
nd6

array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])

#7、创建一个范围在(0,1)之间的长度为12的等差数列
np.linspace(0,1,12)   #均分

array([0. , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
0.90909091, 1. ])

#8、创建一个长度为10的随机数组并排序
a8=np.random.random(10)
np.sort(a8)#从小到大排

array([0.09489862, 0.20160506, 0.21727975, 0.31408097, 0.45653628,
0.50134158, 0.70223996, 0.74757911, 0.8305269 , 0.83557839])

a8.argsort() #从小到大排序,并返回对应下标

array([1, 7, 5, 0, 9, 2, 3, 4, 6, 8], dtype=int64)

a8[a8.argsort()] #根据下标得到数据

array([0.09489862, 0.20160506, 0.21727975, 0.31408097, 0.45653628,
0.50134158, 0.70223996, 0.74757911, 0.8305269 , 0.83557839])

#9、创建一个长度为10的随机数组并将最大值替换为0
nd = np.random.randint(0,10,size = 10)
display(nd)
index_max = nd.argmax()

array([8, 7, 0, 5, 7, 7, 6, 2, 6, 7])

nd[index_max]

8

all_index_max = np.argwhere(nd == nd[index_max]).reshape(-1)
all_index_max

array([0], dtype=int64)

nd[all_index_max] = 0
nd

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

#10、如何根据第3列来对一个5*5矩阵排序?
n10 = np.random.randint(0,100,size=(5,5))
n10

array([[44, 64, 85, 40, 35],
[89, 85, 63, 54, 60],
[24, 67, 4, 43, 9],
[55, 57, 1, 23, 58],
[88, 7, 70, 83, 0]])

n10[:,2] #第三列

array([85, 63, 4, 1, 70])

np.argsort(n10[:,2])  #从小到大排序后的序号数组

array([3, 2, 1, 4, 0], dtype=int64)

n10[np.argsort(n10[:,2])]

array([[55, 57, 1, 23, 58],
[24, 67, 4, 43, 9],
[89, 85, 63, 54, 60],
[88, 7, 70, 83, 0],
[44, 64, 85, 40, 35]])

猜你喜欢

转载自blog.csdn.net/cjhz2333/article/details/129431035