python学习——numpy练习题

import numpy as np
In [9]:
#1 创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1
n = np.zeros(10)
n[4] = 1
print(n)
 
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
In [10]:
#2 创建一个元素为从10到49的ndarray对象
np.arange(10,50)
Out[10]:
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])
In [11]:
#3 将第2题的所有元素位置反转
n3 = np.arange(10,50)
n3[::-1]
Out[11]:
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])
In [29]:
#4 使用np.random.random创建一个10*10的ndarray对象,并打印出最大最小元素
n4 = np.random.random((10,10))
print(n4)
print(np.max(n4),np.min(n4))
 
[[0.87111406 0.86476873 0.86391148 0.78816324 0.93838288 0.59607216
  0.84086973 0.48199116 0.11066871 0.11068288]
 [0.85201476 0.1501309  0.65682176 0.20674656 0.59219751 0.54345428
  0.87809034 0.35371227 0.87620721 0.97906846]
 [0.99045258 0.49207604 0.8109245  0.17304564 0.97809511 0.19944122
  0.35066104 0.57088987 0.59589159 0.90920154]
 [0.13962972 0.81358769 0.95650672 0.87961387 0.06530585 0.63410083
  0.16575957 0.84563097 0.85254561 0.15485708]
 [0.46523209 0.13314196 0.60749123 0.5805076  0.71714643 0.70276885
  0.76393749 0.1839967  0.99657554 0.24095804]
 [0.95328727 0.4174502  0.71184996 0.64976674 0.79155124 0.06004858
  0.42435983 0.9435027  0.43575042 0.96633344]
 [0.64044146 0.60840577 0.57431792 0.98093441 0.41788725 0.11308594
  0.98737901 0.1962544  0.01377043 0.39127853]
 [0.21463967 0.88801687 0.81726285 0.11641557 0.88568505 0.47643872
  0.26798048 0.54929047 0.62710841 0.0651055 ]
 [0.83702334 0.55580473 0.31937827 0.89550998 0.0812324  0.29505207
  0.34792795 0.00443295 0.70254115 0.70284487]
 [0.23769269 0.76225147 0.71931028 0.90641133 0.68028347 0.68391635
  0.50072569 0.5403078  0.26216243 0.8682864 ]]
0.996575541946171 0.004432950968736971
In [41]:
#5 创建一个10*10的ndarray对象,且矩阵边界全为1,里面全为0
# n5 = np.zeros((10,10))
# for i in n5:
#     i[0],i[9] = 1,1
# n5[0] = 1
# n5[9] = 1
# print(n5)
n5 = np.ones((10,10))
n5[1:-1,1:-1] = 0
n5
Out[41]:
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.]])
In [61]:
#6 创建一个每一行都是从0到4的5*5矩阵
np.ones((5,5))*np.arange(5)
Out[61]:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24])
In [72]:
#7 创建一个范围在(0,1)之间的长度为12的等差数列
np.linspace(0,1,12)
Out[72]:
array([0.        , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
       0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
       0.90909091, 1.        ])
In [15]:
#8 创建一个长度为10的随机数组并排序
n8 = np.random.randint(1,10,10)
n8.sort()
print(n8)
 
[1 1 2 5 5 5 5 6 6 8]
In [14]:
#9 创建一个长度为10的随机数组并将最大值替换为0
n9 = np.random.randint(0,150,10)
index = n9.argmax()
n9[index] = 0
n9
Out[14]:
array([43, 33, 48, 50, 72, 27,  3, 33,  9,  0])
In [32]:
#10 如何根据第3列来对一个5*5矩阵排序?
n10 = np.random.randint(0,20,(5,5))
n10_sort = np.argsort(n10[:,3])#对第三列排序并返回索引值
n10[n10_sort]
Out[32]:
array([[ 8,  6,  6,  3, 12],
       [14,  2, 17,  4, 13],
       [19, 15,  4, 14,  9],
       [13,  3, 12, 14,  1],
       [10,  4,  1, 15, 12]])
In [80]:
#11 给定一个4维矩阵,如何得到最后两维的和?
n11 = np.random.randint(0,100,(2,2,2,2))
n11
Out[80]:
array([[[[ 6, 52],
         [15, 41]],

        [[90, 34],
         [41,  8]]],


       [[[45,  8],
         [71, 38]],

        [[82,  8],
         [95, 46]]]])
In [90]:
n11.sum(axis=(2,3))
Out[90]:
array([[114, 173],
       [162, 231]])
In [110]:
#12  给定数组[1, 2, 3, 4, 5],如何得到在这个数组的每个元素之间插入3个0后的新数组?
n12 = np.arange(1,6)
n12_1 = np.zeros(17,dtype=int)
n12_1[::4] = n12
n12_1
Out[110]:
array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5])
In [113]:
n12 = np.arange(1,6).reshape(5,1)
n12_2 = np.zeros((5,3))
np.concatenate([n12,n12_2],axis=1)
Out[113]:
array([[1., 0., 0., 0.],
       [2., 0., 0., 0.],
       [3., 0., 0., 0.],
       [4., 0., 0., 0.],
       [5., 0., 0., 0.]])
In [132]:
#13 给定一个二维矩阵,如何交换其中两行的元素?
n13 = np.random.randint(0,10,(2,2))
n13
Out[132]:
array([[5, 6],
       [3, 7]])
In [133]:
n13[[1,0]]
Out[133]:
array([[3, 7],
       [5, 6]])
In [142]:
#14 创建一个100000长度的随机数组,使用三种方法对其求三次方,并比较所用时间
n14 = np.random.randint(0,1000000,100000)
n14
Out[142]:
array([469994, 274462, 901213, ..., 821177, 810961,  33090])
In [143]:
%timeit n14**3
 
227 µs ± 8.17 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [144]:
%timeit np.power(n14,3)
 
219 µs ± 3.23 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [153]:
n14_1 = np.dot(n14,n14)
%timeit np.dot(n14_1,n14)
 
94.1 µs ± 1.73 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [146]:
#15 创建一个5*3随机矩阵和一个3*2随机矩阵,求矩阵积
n15_1 = np.random.randint(0,100,(5,3))
n15_2 = np.random.randint(0,100,(3,2))
display(n15_1,n15_2)
 
array([[21, 95, 95],
       [63, 18,  5],
       [19,  9, 36],
       [52, 56, 38],
       [57, 32, 70]])
 
array([[64, 57],
       [57, 54],
       [20, 54]])
In [149]:
np.dot(n15_1,n15_2)
Out[149]:
array([[ 8659, 11457],
       [ 5158,  4833],
       [ 2449,  3513],
       [ 7280,  8040],
       [ 6872,  8757]])
In [155]:
#16 矩阵的每一行的元素都减去该行的平均值
n16 = np.random.randint(0,100,(2,2))
n16
Out[155]:
array([[71, 15],
       [80, 47]])
In [166]:
n16_mean = np.mean(n16,axis=1).reshape(2,1)
n16_mean
Out[166]:
array([[43. ],
       [63.5]])
In [167]:
n16 - n16_mean
Out[167]:
array([[ 28. , -28. ],
       [ 16.5, -16.5]])
In [211]:
#17 打印出以下函数(要求使用np.zeros创建8*8的矩阵):
# [[0 1 0 1 0 1 0 1]
#  [1 0 1 0 1 0 1 0]
#  [0 1 0 1 0 1 0 1]
#  [1 0 1 0 1 0 1 0]
#  [0 1 0 1 0 1 0 1]
#  [1 0 1 0 1 0 1 0]
#  [0 1 0 1 0 1 0 1]
#  [1 0 1 0 1 0 1 0]]
n17 = np.zeros((8,8),dtype=int)
n17[::2,1::2]=1
n17[1::2,::2]=1
n17
Out[211]:
array([[0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0]])
In [219]:
#18 正则化一个5*5随机矩阵
# 正则的概念:假设a是矩阵中的一个元素,max/min分别是矩阵元素的最大最小值,
# 则正则化后a = (a - min)/(max - min)
n18 = np.random.randint(0,100,(5,5))
n18_max,n18_min = n18.max(),n18.min()
n18_re = (n18-n18_min)/(n18_max-n18_min)
n18_re
Out[219]:
array([[0.68888889, 0.07777778, 0.85555556, 0.36666667, 0.13333333],
       [0.1       , 0.91111111, 0.67777778, 0.24444444, 0.37777778],
       [0.04444444, 0.45555556, 0.64444444, 0.04444444, 0.04444444],
       [0.86666667, 0.91111111, 0.4       , 0.7       , 0.94444444],
       [0.        , 0.95555556, 0.22222222, 1.        , 0.16666667]])
In [241]:
# 19 将一个一维数组转化为二进制表示矩阵。例如
# [1,2,3]
# 转化为
# [[0,0,1],
# [0,1,0],
# [0,1,1]]
display(1 and 0 ,1 or 1, 1 & 2)
 
0
 
1
 
0
In [237]:
I = np.array([1,2,3])
A = I.reshape(-1,1)
A
Out[237]:
array([[1],
       [2],
       [3]])
In [230]:
B = 2**np.arange(3)
B
Out[230]:
array([1, 2, 4], dtype=int32)
In [231]:
M = A&B
M
Out[231]:
array([[1, 0, 0],
       [0, 2, 0],
       [1, 2, 0]], dtype=int32)
In [232]:
M != 0
Out[232]:
array([[ True, False, False],
       [False,  True, False],
       [ True,  True, False]])
In [235]:
M [M!=0] = 1
M
Out[235]:
array([[1, 0, 0],
       [0, 1, 0],
       [1, 1, 0]], dtype=int32)
In [236]:
M[:,::-1]
Out[236]:
array([[0, 0, 1],
       [0, 1, 0],
       [0, 1, 1]], dtype=int32)
原文地址:https://www.cnblogs.com/bilx/p/11498004.html