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

import numpy as np

ndarry =np.zeros(10)
ndarry
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
ndarry[4]=1
ndarry
array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])

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

ndarray = np.arange(10,50)
ndarray
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题的所有元素位置反转

ndarray[::-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])

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

ndarray = np.random.random((10,10))
ndarray
ndarray.min()
0.009073749212965887

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

a=np.zeros((10,10),dtype=np.int8)
a[[0,-1]]=1
a[:,[0,-1]]=1
a
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)

6、创建一个每一行都是从0到4的5*5矩阵

a=np.array([0,1,2,3,4]*5).reshape(5,5)
a
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的等差数列

a=np.linspace(0,1,12)
a
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的随机数组并排序

a = np.random.randint(1,10,size=10)
a.sort()
a
array([1, 2, 2, 3, 4, 4, 6, 7, 8, 9])

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

a = np.random.randint(1,10,10)

b=a.max()
c=a==b
a[c]=0
a,b,c
(array([3, 5, 0, 7, 1, 2, 7, 0, 5, 7]),9,
     array([False, False,  True, False, False, False, False,  True, False,
            False]))

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

a=np.random.randint(0,25,size=25).reshape(5,5)
a
array([[ 4, 20, 17,  6,  5],
       [ 8, 14, 14, 10,  3],
       [ 7,  5, 12,  7,  4],
       [15,  1,  7,  5, 24],
       [12, 14, 16, 12,  2]])
b=np.argsort(a[:,2])
a[b]
array([[15,  1,  7,  5, 24],
       [ 7,  5, 12,  7,  4],
       [ 8, 14, 14, 10,  3],
       [12, 14, 16, 12,  2],
       [ 4, 20, 17,  6,  5]])

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

a=np.random.randint(0,10,size=(3,4,5,6))
a.sum(axis=-1).sum(axis=-1)
a
array([[[[4, 0, 0, 8, 6, 9],
         [4, 0, 8, 4, 4, 5],
         [3, 7, 2, 4, 2, 7],
         [9, 8, 9, 2, 3, 1],
         [8, 1, 4, 2, 5, 9]],

        [[7, 5, 5, 7, 6, 2],
         [3, 5, 3, 4, 7, 1],
         [0, 5, 7, 7, 6, 7],
         [5, 8, 7, 9, 8, 3],
         [6, 9, 2, 6, 0, 5]],

        [[9, 2, 7, 9, 0, 7],
         [1, 4, 7, 5, 4, 3],
         [6, 8, 0, 2, 8, 4],
         [8, 4, 8, 7, 4, 3],
         [0, 9, 2, 2, 6, 7]],

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

       [[[0, 4, 8, 9, 7, 6],
          [0, 0, 7, 0, 8, 3],
          [3, 7, 7, 4, 7, 8],
          [5, 1, 7, 5, 3, 7],
          [8, 9, 0, 4, 2, 6]],

        [[1, 9, 1, 2, 7, 6],
         [6, 4, 4, 3, 9, 5],
         [4, 1, 5, 8, 9, 5],
         [0, 0, 2, 8, 9, 5],
         [6, 3, 0, 8, 0, 9]],

        [[5, 4, 9, 2, 9, 8],
         [2, 7, 4, 3, 5, 8],
         [8, 8, 0, 4, 4, 8],
         [7, 5, 1, 6, 1, 1],
         [1, 8, 1, 0, 4, 4]],

        [[7, 6, 6, 4, 8, 2],
         [2, 1, 1, 1, 0, 7],
         [5, 1, 6, 2, 3, 5],
         [2, 4, 6, 0, 8, 9],
         [3, 6, 5, 3, 1, 6]]],


       [[[1, 8, 1, 7, 2, 5],
         [3, 0, 9, 3, 8, 0],
         [5, 2, 1, 0, 8, 4],
         [4, 1, 3, 8, 4, 3],
         [4, 8, 8, 2, 6, 2]],

        [[7, 6, 2, 5, 9, 7],
         [2, 9, 1, 2, 5, 9],
         [3, 0, 2, 5, 5, 0],
         [6, 0, 1, 8, 4, 3],
         [5, 4, 5, 6, 2, 1]],

        [[9, 5, 0, 5, 0, 7],
         [0, 4, 8, 4, 4, 8],
         [6, 2, 3, 9, 3, 7],
         [7, 7, 1, 8, 3, 5],
         [0, 4, 7, 6, 9, 7]],

        [[2, 1, 4, 6, 7, 8],
         [8, 6, 0, 5, 8, 1],
         [9, 4, 8, 2, 8, 5],
         [0, 6, 2, 3, 9, 6],
         [8, 7, 4, 2, 7, 6]]]])

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

a=np.arange(1,6)
b=np.zeros(17,dtype=np.int8)
b[::4]=a
b
array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5], dtype=int8)

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

a=np.arange(0,20).reshape(4,5)
a
a[[1,0,2,3]]
a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])

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

import time
a=np.random.randn(1000000)
a

t1 = time.time()
np.power(a,3)

time= time.time()-t1
time
0.0468747615814209

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

a=np.random.randint(0,10,(5,3))

b=np.random.randint(0,10,(3,2))
a,b
np.matmul(a,b)
array([[ 48,  50],
       [171, 101],
       [124,  62],
       [105,  63],
       [ 75,  67]])

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

a=np.random.randint(0,10,(3,3))
a
b=a.mean(axis=1).reshape(3,1)
a-b
array([[ 2.66666667, -1.33333333, -1.33333333],
       [-3.        ,  3.        ,  0.        ],
       [-1.33333333,  0.66666667,  0.66666667]])

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

a=np.zeros((8,8),dtype='int')
a[::2,1::2]=1
a[1::2,::2]=1
a
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)

a=np.random.randint(0,10,size=(5,5))
amin=a.min()
amax=a.max()
(a-amin)/(amax-amin)
array([[0.44444444, 0.11111111, 0.11111111, 0.33333333, 0.88888889],
       [0.        , 0.88888889, 0.77777778, 1.        , 0.11111111],
       [0.33333333, 0.55555556, 0.66666667, 0.33333333, 0.11111111],
       [0.55555556, 0.33333333, 1.        , 0.77777778, 0.33333333],
       [0.11111111, 0.33333333, 0.11111111, 0.88888889, 0.77777778]])

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

num = 4
n1 = np.arange(num)
n1
n2 = np.zeros(shape=(num,num),dtype='int')
n2
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
for i in range(num):
    n = n1[i]
    for j in range(num-1,-1,-1):
        n2[i][j] = n%2
        n = n//2
n2[1::]
array([[0, 0, 0, 1],
       [0, 0, 1, 0],
       [0, 0, 1, 1]])

20、实现冒泡排序法

arr = np.random.randint(1,10,size=10)
for i in range(1, len(arr)):
    for j in range(0, len(arr)-i):
        if arr[j] > arr[j+1]:
            arr[j], arr[j + 1] = arr[j + 1], arr[j]
arr
array([2, 2, 3, 5, 5, 6, 7, 8, 9, 9])

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://wakemeupnow.cn/article/numpy/