20道习题:练练手(附答案)

1、创建一个长度为10的一维全为0的ndarray对象,然后让第5个元素等于1

In [6]:
import numpy as np 
np.__version__
Out[6]:
'1.13.1'
In [7]:
nd= np.array([0,0,0,0.0,0,0,0,0,0])
nd
Out[7]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
In [8]:
nd[4] = 5
nd
Out[8]:
array([ 0.,  0.,  0.,  0.,  5.,  0.,  0.,  0.,  0.])

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

In [9]:
nd2=np.arange(10,49,5)
nd2
Out[9]:
array([10, 15, 20, 25, 30, 35, 40, 45])

3、将第2题的所有元素位置反转

In [10]:
nd3=nd2[::-1]
nd3
Out[10]:
array([45, 40, 35, 30, 25, 20, 15, 10])

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

In [11]:
nd4=np.random.random((10,10)) # 生成0-1之间的浮点数
nd4
Out[11]:
array([[ 0.15764428,  0.71596328,  0.81808397,  0.81675306,  0.70382255,
         0.78524804,  0.21928811,  0.43166498,  0.49184857,  0.07405726],
       [ 0.43535449,  0.69695436,  0.25976013,  0.65293315,  0.12796236,
         0.89170976,  0.89538566,  0.7661741 ,  0.06041598,  0.75495737],
       [ 0.39672305,  0.48224634,  0.29196171,  0.01158065,  0.27270422,
         0.9055367 ,  0.77696274,  0.55111361,  0.36990923,  0.44839469],
       [ 0.49302039,  0.43910028,  0.69551093,  0.38485437,  0.59290733,
         0.60957991,  0.60690416,  0.7290093 ,  0.153923  ,  0.68158969],
       [ 0.18287042,  0.998713  ,  0.21240307,  0.76586074,  0.99363589,
         0.62133375,  0.47339622,  0.94654607,  0.1572087 ,  0.95343159],
       [ 0.57546028,  0.96684773,  0.91605759,  0.76285641,  0.39246144,
         0.5048398 ,  0.20537326,  0.03821489,  0.93846779,  0.67465357],
       [ 0.50036711,  0.01044893,  0.34497477,  0.86604052,  0.01158555,
         0.61679012,  0.18245735,  0.93472696,  0.95285364,  0.25008413],
       [ 0.02460187,  0.71865045,  0.88454367,  0.89606069,  0.007162  ,
         0.48205852,  0.8488491 ,  0.30527469,  0.09745148,  0.41324897],
       [ 0.85682063,  0.12847062,  0.20754213,  0.72276057,  0.55677094,
         0.4177689 ,  0.21974381,  0.27618731,  0.86658251,  0.13244381],
       [ 0.08830751,  0.86493876,  0.97546856,  0.90726232,  0.96354161,
         0.79810109,  0.41307841,  0.65814493,  0.93662478,  0.81531833]])
In [12]:
Zmin, Zmax = nd4.min(), nd4.max()
Zmin, Zmax
Out[12]:
(0.007161996597249809, 0.99871299576647787)

5、创建一个10*10的ndarray对象,且矩阵边界全为1,里面全为0

In [13]:
nd5 = np.ones((10,10))
nd5[1:-1,1:-1] = 0
nd5
Out[13]:
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矩阵

In [14]:
nd6=np.arange(0,25,1)
nd6.resize((5,5))
nd61=np.arange(0,5,1)
nd6[0:5]=nd61
nd6
Out[14]:
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的等差数列

In [15]:
nd7=np.linspace(0,1,12)
nd7
Out[15]:
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的随机数组并排序

In [16]:
nd8=np.random.random(10)
nd8.sort()
nd8
Out[16]:
array([ 0.13239674,  0.24199985,  0.28860574,  0.31665663,  0.33797971,
        0.49389128,  0.54173172,  0.66709575,  0.87428616,  0.93431918])

9、创建一个长度为10的随机数组并将最大值替换为0

In [17]:
nd9=np.random.random(10)
nd9[nd9.argmax()]=0
nd9
Out[17]:
array([ 0.37927771,  0.74980137,  0.45306485,  0.        ,  0.38631354,
        0.03016797,  0.68861177,  0.78478335,  0.12929193,  0.38233788])

10、如何根据第3列来对一个5*5矩阵排序?

In [18]:
nd10=np.random.randint(0,20,size=(5,5))
nd10
Out[18]:
array([[ 4, 17,  7, 16,  7],
       [ 4, 11, 14, 19,  1],
       [10,  6,  1, 17,  0],
       [ 0, 10, 17, 13, 15],
       [ 1, 11,  3,  6, 16]])
In [19]:
sort_ind = np.argsort(nd10[:,3]) # 排序并且返回排序以后的下标序列
sort_ind
Out[19]:
array([4, 3, 0, 2, 1], dtype=int64)
In [20]:
nd10[sort_ind]
Out[20]:
array([[ 1, 11,  3,  6, 16],
       [ 0, 10, 17, 13, 15],
       [ 4, 17,  7, 16,  7],
       [10,  6,  1, 17,  0],
       [ 4, 11, 14, 19,  1]])

11、给定一个4维矩阵,如何得到最后两维的和?

In [21]:
nd11=np.random.randint(0,10,size=(2,3,2,2))
nd11
Out[21]:
array([[[[3, 3],
         [1, 5]],

        [[9, 9],
         [4, 3]],

        [[6, 4],
         [9, 0]]],


       [[[1, 8],
         [8, 7]],

        [[9, 0],
         [8, 2]],

        [[0, 1],
         [0, 1]]]])
In [22]:
nd11.sum(axis=-1)
nd11.sum(axis=-2)
nd11.sum(axis=(-1,-2))
Out[22]:
array([[12, 25, 19],
       [24, 19,  2]])

12、给定数组[1, 2, 3, 4, 5],如何得到在这个数组的每个元素之间插入3个0后的新数组?

In [23]:
nd12=[1,2,3,4,5]
nd12
Out[23]:
[1, 2, 3, 4, 5]
In [24]:
nd12_v=np.vstack(nd12)
nd12_v
Out[24]:
array([[1],
       [2],
       [3],
       [4],
       [5]])
In [25]:
nd12_z=np.zeros((5,3),dtype="int32")
nd12_z
Out[25]:
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])
In [26]:
np.concatenate([nd12_v,nd12_z],axis=1)
Out[26]:
array([[1, 0, 0, 0],
       [2, 0, 0, 0],
       [3, 0, 0, 0],
       [4, 0, 0, 0],
       [5, 0, 0, 0]])

13、给定一个二维矩阵,如何交换其中两行的元素?

In [27]:
nd13=np.random.randint(0,20,size=(3,4))
nd13
Out[27]:
array([[ 1, 12, 16, 13],
       [ 8, 13, 10,  4],
       [ 7,  7,  2,  5]])
In [28]:
nd13[[0,2,1]]
Out[28]:
array([[ 1, 12, 16, 13],
       [ 7,  7,  2,  5],
       [ 8, 13, 10,  4]])

14、创建一个100000长度的随机数组,使用两种方法对其求三次方,并比较所用时间

In [29]:
nd14=np.random.randint(0,100000,5)
nd14
Out[29]:
array([46129, 89120, 29541, 79550, 66737])
In [30]:
%timeit np.power(nd14, 3)
1.66 µs ± 131 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [31]:
nd14_2=np.dot(nd14,nd14)
%timeit np.dot(nd14_2,nd14)
2.51 µs ± 253 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

15、创建一个53随机矩阵和一个32随机矩阵,求矩阵积

In [32]:
nd15_1=np.random.randint(0,10,size=(5,3))
nd15_1
Out[32]:
array([[6, 7, 4],
       [0, 3, 8],
       [0, 0, 3],
       [0, 3, 2],
       [4, 1, 2]])
In [33]:
nd15_2=np.random.randint(0,10,size=(3,2))
nd15_2
Out[33]:
array([[1, 5],
       [1, 9],
       [6, 2]])
In [34]:
np.dot(nd15_1,nd15_2)
Out[34]:
array([[ 37, 101],
       [ 51,  43],
       [ 18,   6],
       [ 15,  31],
       [ 17,  33]])

16、矩阵的每一行的元素都减去该行的平均值

In [35]:
nd16=np.random.randint(0,10,size=(3,2))
nd16
Out[35]:
array([[2, 7],
       [6, 9],
       [2, 9]])
In [36]:
nd16_1=np.mean(nd16,axis=1)
nd16_2=np.vstack(nd16_1)
nd16_2
Out[36]:
array([[ 4.5],
       [ 7.5],
       [ 5.5]])
In [37]:
nd16-nd16_2
Out[37]:
array([[-2.5,  2.5],
       [-1.5,  1.5],
       [-3.5,  3.5]])

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]]

In [38]:
nd17=np.zeros((8,8),dtype="int32")
nd17
Out[38]:
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0]])
In [39]:
nd17[1::2,::2] = 1
nd17[::2,1::2] = 1
nd17
Out[39]:
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]])

18、正则化一个5*5随机矩阵 正则的概念:假设a是矩阵中的一个元素,max/min分别是矩阵元素的最大最小值,则正则化后a = (a - min)/(max - min)

In [40]:
nd18 = np.random.random((5,5))
max, min =nd18.max(),nd18.min()
nd18 = (nd18 - min)/(max - min)
nd18
Out[40]:
array([[ 0.25144383,  0.82645318,  0.        ,  0.18497809,  0.66760464],
       [ 0.98185414,  0.09698211,  0.40000897,  0.32858921,  1.        ],
       [ 0.20143467,  0.56118396,  0.53422648,  0.7448601 ,  0.79003777],
       [ 0.63127953,  0.24049893,  0.84784467,  0.01238311,  0.80379845],
       [ 0.46492319,  0.0671232 ,  0.71125054,  0.04335297,  0.05462583]])

19、将一个一维数组转化为二进制表示矩阵。例如 [1,2,3] 转化为 [[0,0,1], [0,1,0], [0,1,1]]

In [41]:
1 and 0
Out[41]:
0
In [42]:
1 or 0
Out[42]:
1
In [43]:
1 & 2
Out[43]:
0
In [44]:
# 60 = 1*2^5 + 1*2^4 + 1*2^3 +0*2^2+0*2^1+ 1*2^0
# 111001 & 2^3
#2 = 010
# 3 = 011
# 011
# 010
1 & 2**0
1 & 2**1
1 & 2**2
3 & 2**0
3 & 2**1
3 & 2**2
Out[44]:
0
In [45]:
I = np.array([1,2,3])
I
Out[45]:
array([1, 2, 3])
In [46]:
A = I.reshape((-1,1))
A
Out[46]:
array([[1],
       [2],
       [3]])
In [47]:
B = 2**np.arange(3)
B
Out[47]:
array([1, 2, 4], dtype=int32)
In [48]:
M = A & B
M
Out[48]:
array([[1, 0, 0],
       [0, 2, 0],
       [1, 2, 0]], dtype=int32)
In [49]:
M != 0
Out[49]:
array([[ True, False, False],
       [False,  True, False],
       [ True,  True, False]], dtype=bool)
In [50]:
M[M!=0] = 1
M
Out[50]:
array([[1, 0, 0],
       [0, 1, 0],
       [1, 1, 0]], dtype=int32)
In [51]:
M[:,::-1]
Out[51]:
array([[0, 0, 1],
       [0, 1, 0],
       [0, 1, 1]], dtype=int32)

20、实现冒泡排序法

In [52]:
nd20=[1,3,4,2,78,5,0,8]
for i in range(len(nd20) - 1):  
    for j in range(len(nd20)- 1 - i):  
        if nd20[j] > nd20[j+1]:  
            nd20[j],nd20[j+1] = nd20[j+1],nd20[j]
nd20
Out[52]:
[0, 1, 2, 3, 4, 5, 8, 78]

猜你喜欢

转载自blog.csdn.net/qq_29784441/article/details/80858771