基于Jupyer +python 完成20道numpy练习题

20道numpy练习题


import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
 
%matplotlib inline
    
    
np.__version__
'1.14.3'

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

s1=np.zeros(shape=10)
s1
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
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([38, 44, 13, 44, 48, 17, 30, 28, 14, 17])
np.linspace(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、将第2题的所有元素位置反转

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.28354791, 0.53881869, 0.73297724, 0.58398453, 0.67710008,
        0.59168129, 0.42998347, 0.61881661, 0.97212201, 0.81561104],
       [0.34912502, 0.6594156 , 0.25213939, 0.96759981, 0.13290112,
        0.4813469 , 0.42313677, 0.22163765, 0.64086143, 0.45090776],
       [0.20370509, 0.5747173 , 0.57938231, 0.97831442, 0.72606308,
        0.3602857 , 0.47852929, 0.59045362, 0.39273084, 0.90968796],
       [0.43191038, 0.11233406, 0.76919378, 0.1958468 , 0.64861517,
        0.32179814, 0.87071915, 0.3597473 , 0.3294416 , 0.1928191 ],
       [0.91285387, 0.89655008, 0.78937774, 0.12250336, 0.16096717,
        0.32301366, 0.99849192, 0.27273704, 0.56732485, 0.69054795],
       [0.43237872, 0.79010462, 0.70288099, 0.94193154, 0.85444242,
        0.78459454, 0.09087923, 0.33960947, 0.89746055, 0.41448452],
       [0.90024892, 0.25604412, 0.21635101, 0.59990646, 0.15179373,
        0.41992719, 0.61414268, 0.58502172, 0.95240914, 0.15525847],
       [0.97721313, 0.5769531 , 0.13814214, 0.67663621, 0.5212391 ,
        0.75455375, 0.28823995, 0.29543965, 0.32789298, 0.44977329],
       [0.47226262, 0.51397234, 0.80356646, 0.73003842, 0.30801848,
        0.56275802, 0.36678079, 0.17652075, 0.91430428, 0.94075414],
       [0.81865326, 0.74960831, 0.49145598, 0.91527198, 0.48563009,
        0.01607068, 0.23606723, 0.44710665, 0.69705199, 0.87911141]])

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

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

import numpy as np
nd = np.zeros(shape=(10,10),dtype = np.int8)
nd[0,9]=1

nd[:,[0,9]] = 1
nd
array([[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, 0, 0, 0, 0, 0, 0, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1]], dtype=int8)
a5=np.ones((10,10))
a5
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
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矩阵

#创建一个每一行都是从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_l=np.arange(0,5,1)
nd6_l
array([0, 1, 2, 3, 4])
nd6=np.arange(0,25).reshape(5,5)
nd6
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
nd6[0:5]=nd6_l
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)
a8
array([0.37785131, 0.84057534, 0.38809694, 0.79531588, 0.27602274,
       0.49318451, 0.37381759, 0.10048736, 0.21532944, 0.03181141])
np.sort(a8)
array([0.03181141, 0.10048736, 0.21532944, 0.27602274, 0.37381759,
       0.37785131, 0.38809694, 0.49318451, 0.79531588, 0.84057534])
a8.argsort()
array([9, 7, 8, 4, 6, 0, 2, 5, 3, 1], dtype=int64)
a8[a8.argsort()]
array([0.03181141, 0.10048736, 0.21532944, 0.27602274, 0.37381759,
       0.37785131, 0.38809694, 0.49318451, 0.79531588, 0.84057534])

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

#创建一个长度为10的随机数组并将最大值替换为0
nd = np.random.randint(0,10,size = 10)
display(nd)
index_max = nd.argmax()
array([2, 4, 1, 5, 6, 6, 8, 1, 0, 4])
nd[index_max]
8
all_index_max=np.argwhere(nd == nd[index_max]).reshape(-1)
all_index_max
array([6], dtype=int64)
nd[all_index_max] = -100
nd
array([   2,    4,    1,    5,    6,    6, -100,    1,    0,    4])

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

n10=np.random.randint(0,100,size=(5,5))
n10
array([[90, 38, 98, 24, 13],
       [59,  8, 31, 73, 28],
       [46, 31, 68, 42, 79],
       [34, 74, 38, 99, 31],
       [91, 92, 15, 61, 89]])
n10[:,2]
array([98, 31, 68, 38, 15])
np.argsort(n10[:,2])
array([4, 1, 3, 2, 0], dtype=int64)
n10[np.argsort(n10[:,2])]
array([[91, 92, 15, 61, 89],
       [59,  8, 31, 73, 28],
       [34, 74, 38, 99, 31],
       [46, 31, 68, 42, 79],
       [90, 38, 98, 24, 13]])

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

n11=np.random.randint(0,100,size=(2,3,3,3))
n11
array([[[[24, 43, 37],
         [49, 90, 81],
         [26,  6, 76]],

        [[57, 61, 71],
         [ 3,  3, 41],
         [44, 73, 11]],

        [[30, 10,  3],
         [ 9, 83, 25],
         [57, 98, 56]]],


       [[[37,  0, 21],
         [54, 27, 32],
         [72, 55,  7]],

        [[27, 46, 55],
         [39, 18,  0],
         [39, 98, 33]],

        [[40, 92, 93],
         [26, 64, 35],
         [13,  3, 98]]]])
n11.sum(axis=(2,3))
array([[432, 364, 371],
       [305, 355, 464]])
n11.sum(axis=(-1,-2))
array([[432, 364, 371],
       [305, 355, 464]])

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

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

nd1=np.arange(1,6)
nd2=np.zeros(shape=17,dtype=int)
nd2[::4]=nd1
nd2
array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5])
n12=np.array([1,2,3,4,5]).reshape(5,1)
n12
array([[1],
       [2],
       [3],
       [4],
       [5]])
n12_1=np.zeros((5,3))
n12_1
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
n12=np.concatenate([n12,n12_1],axis=1)
n12
array([[1., 0., 0., 0.],
       [2., 0., 0., 0.],
       [3., 0., 0., 0.],
       [4., 0., 0., 0.],
       [5., 0., 0., 0.]])
n12.reshape(-1)
array([1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4., 0., 0., 0., 5.,
       0., 0., 0.])

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

n13=np.random.randint(0,100,size=(3,3))
n13
array([[61, 81, 26],
       [61, 36, 44],
       [86, 91, 47]])
n13[[1,0,2]]
array([[61, 36, 44],
       [61, 81, 26],
       [86, 91, 47]])

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

n14=np.random.random(size=100000)
n14
array([0.23721659, 0.01376023, 0.34653253, ..., 0.63375536, 0.03298164,
       0.74823954])
n14=np.random.randint(0,100000,(5))
n14
array([20436, 31710, 57050, 98014, 46527])
%timeit np.power(n14,3)
701 ns ± 10.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit n14**3
761 ns ± 47.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
n14_2 = np.dot(n14,n14)
%timeit np.dot(n14_2,n14)
754 ns ± 35.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

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

n15_53=np.random.randint(0,100,size=(5,3))
n15_53
array([[89, 63, 59],
       [97, 14, 62],
       [65, 83, 44],
       [90, 11, 71],
       [66, 82, 94]])
n15_32=np.random.randint(0,100,(3,2))
n15_32
array([[92, 26],
       [74, 52],
       [50,  0]])
np.dot(n15_53,n15_32)
array([[15800,  5590],
       [13060,  3250],
       [14322,  6006],
       [12644,  2912],
       [16840,  5980]])

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

n16=np.random.randint(0,10,(3,3))
n16
array([[0, 9, 5],
       [9, 0, 0],
       [2, 2, 2]])
n16_1=n16.mean(axis=1).reshape(3,1)  #对行求均值
n16_1
array([[4.66666667],
       [3.        ],
       [2.        ]])
n16-n16_1
array([[-4.66666667,  4.33333333,  0.33333333],
       [ 6.        , -3.        , -3.        ],
       [ 0.        ,  0.        ,  0.        ]])

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

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

nd=np.ones(shape=(8,8),dtype=int)
nd[::2,::2]=0
nd[1::2,1::2]=0
nd

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]])
n17=np.zeros(shape=(8,8))
n17
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.]])
n17[1::2,::2]=1
n17[::2,1::2]=1
n17
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、正则化一个55随机矩阵
【正则的概念:假设a是矩阵中的一个元素,max/min分别是矩阵元素的最大最小值,则正则化后a = (a - min)/(max - min)】18、正则化一个5
5随机矩阵

'''18、正则化一个5*5随机矩阵
【正则的概念:假设a是矩阵中的一个元素,max/min分别是矩阵元素的最大最小值,则正则化后a = (a - min)/(max - min)
'''
nd=np.random.randint(0,100,size=(5,5))
v_min=nd.min()
v_max=nd.max()

#0~1
(nd-v_min)/(v_max-v_min)
array([[1.        , 0.36363636, 0.71717172, 0.91919192, 0.18181818],
       [0.54545455, 0.65656566, 0.12121212, 0.22222222, 0.        ],
       [0.44444444, 0.73737374, 0.42424242, 0.17171717, 0.57575758],
       [0.45454545, 0.16161616, 0.81818182, 0.84848485, 0.58585859],
       [0.12121212, 0.87878788, 0.24242424, 0.76767677, 0.44444444]])
n18=np.random.randint(0,100,(5,5))
n18
array([[74, 61,  9, 49, 45],
       [96, 95, 13, 80, 25],
       [75, 28, 13, 57, 41],
       [78, 73, 52, 96, 59],
       [27, 82,  8, 38, 29]])
nmax,nmin=n18.max(),n18.min()
nmax,nmin
(96, 8)
n18=(n18-nmin)/(nmax-nmin)
n18
array([[0.75      , 0.60227273, 0.01136364, 0.46590909, 0.42045455],
       [1.        , 0.98863636, 0.05681818, 0.81818182, 0.19318182],
       [0.76136364, 0.22727273, 0.05681818, 0.55681818, 0.375     ],
       [0.79545455, 0.73863636, 0.5       , 1.        , 0.57954545],
       [0.21590909, 0.84090909, 0.        , 0.34090909, 0.23863636]])

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

1 and 0
0
1 or 1
1
1 & 2
0
I=np.array([1,2,3])
I
array([1, 2, 3])
A=I.reshape(-1,1)
A
array([[1],
       [2],
       [3]])
B=2**np.arange(3)
B
array([1, 2, 4], dtype=int32)
M=A&B
M
array([[1, 0, 0],
       [0, 2, 0],
       [1, 2, 0]], dtype=int32)
M != 0
array([[ True, False, False],
       [False,  True, False],
       [ True,  True, False]])
M[M !=0 ]=1
M
array([[1, 0, 0],
       [0, 1, 0],
       [1, 1, 0]], dtype=int32)
M[:,::-1]
array([[0, 0, 1],
       [0, 1, 0],
       [0, 1, 1]], dtype=int32)

20、实现冒泡排序法

n20=[19,60,66,71,54,57,1,24,82,39]
for i in range (len(n20)-1):
    for j in range (len(n20)-i-1):
        if n20[j]>n20[j+1]:
            n20[j],n20[j+1]=n20[j+1],n20[j]
n20
[1, 19, 24, 39, 54, 57, 60, 66, 71, 82]
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值