文章目录

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

import numpy as np
a = np.zeros(shape=10,dtype='int8')
a

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

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

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

b
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])
b[::-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对象,并打印出最大最小元素

c = np.random.random(size=(10,10))
c
array([[0.12093975, 0.3267446 , 0.32511303, 0.98392747, 0.13484395,
        0.37976155, 0.92266309, 0.51045066, 0.94734899, 0.27013075],
       [0.22105076, 0.55577113, 0.0953467 , 0.02715877, 0.58898711,
        0.94926978, 0.37500521, 0.93043798, 0.43872141, 0.46909585],
       [0.33902826, 0.57696865, 0.16706052, 0.46399826, 0.79887315,
        0.87696158, 0.46389537, 0.31834363, 0.62135635, 0.96444487],
       [0.01753888, 0.89872472, 0.95394932, 0.04530442, 0.4804764 ,
        0.90437561, 0.18184513, 0.87611216, 0.76307676, 0.59577148],
       [0.88609283, 0.57264267, 0.06456211, 0.55960739, 0.05683746,
        0.28712179, 0.82489792, 0.3433044 , 0.08050466, 0.87076249],
       [0.0720682 , 0.78059738, 0.51433292, 0.91360016, 0.11120904,
        0.69357684, 0.92410724, 0.94424784, 0.61191778, 0.22802597],
       [0.23066127, 0.24275318, 0.7704362 , 0.47635963, 0.82819756,
        0.34623774, 0.87064341, 0.35837979, 0.71846938, 0.75084529],
       [0.24568906, 0.49863974, 0.6156872 , 0.86632902, 0.70506218,
        0.82317306, 0.17853396, 0.52638784, 0.49587969, 0.10768218],
       [0.87062351, 0.14262037, 0.69633587, 0.01229725, 0.25732244,
        0.63462171, 0.058154  , 0.67631851, 0.35825579, 0.48992349],
       [0.38399769, 0.87204559, 0.77859537, 0.22979291, 0.15440065,
        0.40481701, 0.01454451, 0.53717479, 0.19480013, 0.70916041]])
cmax = c.max()
cmax
0.9839274727793249
cmin = c.min()
cmin
0.012297252184459095

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

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

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

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

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

i =np.random.randint(1,100,10)
i
array([71, 11, 85, 14, 41, 66, 66, 38, 38, 47])
i.sort()
i
array([11, 14, 38, 38, 41, 47, 66, 66, 71, 85])

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

j = np.random.randint(1,100,10)
j
array([71, 98,  6, 77, 89, 18, 32, 33, 37,  6])
max_index = j.argmax()
max_index
1
j[max_index] = 0
j
array([71,  0,  6, 77, 89, 18, 32, 33, 37,  6])

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

k = np.random.randint(0,20,(5,5))
k
array([[19, 11,  5, 10, 14],
       [ 0, 11, 16, 10,  8],
       [ 4, 13,  8,  1,  0],
       [ 9,  2, 10, 13, 11],
       [ 5, 13,  7,  6,  3]])
#对第三列排序并返回索引值
k_sort = np.argsort(k[:,2])
k_sort
k[k_sort]
array([[19, 11,  5, 10, 14],
       [ 5, 13,  7,  6,  3],
       [ 4, 13,  8,  1,  0],
       [ 9,  2, 10, 13, 11],
       [ 0, 11, 16, 10,  8]])

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

l = np.random.randint(1,100,size=(2,3,3,3))
l
array([[[[10, 51, 43],
         [92, 87,  6],
         [62, 64, 22]],

        [[57, 92, 85],
         [89, 95, 13],
         [31, 37, 79]],

        [[15, 86,  1],
         [67, 83, 56],
         [42, 20,  6]]],


       [[[31,  8, 62],
         [55, 39, 41],
         [70, 28, 95]],

        [[50, 17, 68],
         [12, 73, 64],
         [94, 73,  6]],

        [[34, 58, 11],
         [41, 42, 66],
         [77, 71, 80]]]])
l.sum(axis=(2,3))
array([[437, 578, 376],
       [429, 457, 480]])
l.sum(axis=(-1,-2))
array([[437, 578, 376],
       [429, 457, 480]])

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

m = np.arange(1,6)
m
array([1, 2, 3, 4, 5])
n = np.zeros(shape=17,dtype='int8')
n
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int8)
n[::4]=m
n
array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5], dtype=int8)

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

o = np.random.randint(1,100,size=(3,3))
o
array([[70, 60,  9],
       [84, 19, 62],
       [83, 57, 20]])
o[[2,0]]=o[[0,2]]
o
array([[83, 57, 20],
       [84, 19, 62],
       [70, 60,  9]])

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

p = np.random.random(size=100000)
p
array([0.17630027, 0.16443041, 0.41460157, ..., 0.23775531, 0.28457956,
       0.58646857])
%time np.power(p,3)
Wall time: 3.99 ms
array([0.00547973, 0.00444576, 0.07126771, ..., 0.01343973, 0.02304683,
       0.20171316])
%timeit p**3
2.92 ms ± 41.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

15、创建一个5 * 3随机矩阵和一个3*2随机矩阵,求矩阵积

q = np.random.randint(1,100,size=(5,3))
q
array([[20, 42, 46],
       [89, 91, 40],
       [51, 64, 60],
       [15, 86, 77],
       [93, 81, 50]])
r = np.random.randint(1,100,size=(3,2))
r
array([[ 2, 36],
       [22, 66],
       [23,  2]])
np.dot(q,r)
array([[2022, 3584],
       [3100, 9290],
       [2890, 6180],
       [3693, 6370],
       [3118, 8794]])

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

s = np.random.randint(1,100,(3,3))#对行求均值
s
array([[91, 52, 66],
       [52, 68, 85],
       [93, 59, 17]])
s_line_mean = s.mean(axis=1).reshape(3,1)
s_line_mean
array([[69.66666667],
       [68.33333333],
       [56.33333333]])
s-s_line_mean
array([[ 21.33333333, -17.66666667,  -3.66666667],
       [-16.33333333,  -0.33333333,  16.66666667],
       [ 36.66666667,   2.66666667, -39.33333333]])

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]]
t = np.ones(shape = (8,8),dtype=int)
t
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]])
t[::2,::2] = 0
t
array([[0, 1, 0, 1, 0, 1, 0, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 1, 1, 1, 1, 1, 1, 1]])
t[1::2,1::2]=0
t
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)

u = np.random.randint(1,100,(5,5))
u
array([[77,  8,  1, 97,  7],
       [51, 35, 49, 10,  5],
       [28, 29, 81, 12, 90],
       [32, 87, 37, 15, 86],
       [29, 12, 38, 28, 39]])
umax = u.max()
umax
97
umin = u.min()
umin
1
v = (u-umin)/(umax-umin)
v
array([[0.79166667, 0.07291667, 0.        , 1.        , 0.0625    ],
       [0.52083333, 0.35416667, 0.5       , 0.09375   , 0.04166667],
       [0.28125   , 0.29166667, 0.83333333, 0.11458333, 0.92708333],
       [0.32291667, 0.89583333, 0.375     , 0.14583333, 0.88541667],
       [0.29166667, 0.11458333, 0.38541667, 0.28125   , 0.39583333]])

19、实现冒泡排序法

def bubble_sort(alist):
    n = len(alist)
    for j in range(n-1):
        count = 0
        for i in range(0, n-1-j):
            if alist[i] > alist[i+1]:
                alist[i],alist[i+1] = alist[i+1], alist[i]
                count += 1
        if 0 == count:
            return
li = [1,4,8,7,3,6,2,5]
print(li)
bubble_sort(li)
print(li)
[1, 4, 8, 7, 3, 6, 2, 5]
[1, 2, 3, 4, 5, 6, 7, 8]

20、实现快速排序法

def quick_sort(alist, first, last):
    if first >= last:
        return
    mid_value = alist[first]
    low = first
    high = last
    while low < high:
        while low < high and alist[high] >= mid_value:
            high -= 1
        alist[low] = alist[high]while low <high and alist[low] < mid_value:
            low += 1
        alist[high] = alist[low]
    alist[low] = mid_value
    quick_sort(alist, first, low-1)
    quick_sort(alist, low+1, last)
li = [1,4,8,7,3,6,2,5]
print(li)
quick_sort(li, 0, len(li)-1)
print(li)
[1, 4, 8, 7, 3, 6, 2, 5]
[1, 2, 3, 4, 5, 6, 7, 8]

21、实现希尔排序法

def shell_sort(alist):
    n = len(alist)
    gap = n // 2
    while gap > 0:
        for j in range(gap, n):
            i = j
            while i > 0:
                if alist[i] < alist[i-gap]:
                    alist[i], alist[i-gap] = alist[i-gap], alist[i]
                    i -= gap
                else:
                    break
        gap //= 2
li = [1,4,8,7,3,6,2,5]
print(li)
shell_sort(li)
print(li)
[1, 4, 8, 7, 3, 6, 2, 5]
[1, 2, 3, 4, 5, 6, 7, 8]

22、实现插入排序法

def insert_sort(alist):
    n = len(alist)
    for j in range(1, n):
        i = j
        while i > 0:
            if alist[i] < alist[i-1]:
                alist[i], alist[i-1] = alist[i-1], alist[i]
                i -= 1
            else:
                break
li = [1,4,8,7,3,6,2,5]
print(li)
insert_sort(li)
print(li)
[1, 4, 8, 7, 3, 6, 2, 5]
[1, 2, 3, 4, 5, 6, 7, 8]

23、实现归并排序法

def merge_sort(alist):
    n = len(alist)
    if n <= 1:
        return alist
    mid = n//2
    left_li = merge_sort(alist[:mid])
    right_li = merge_sort(alist[mid:])
    left_pointer, right_pointer = 0, 0
    result = []
    while left_pointer < len(left_li) and right_pointer < len(right_li):
        if left_li[left_pointer] <=  right_li[right_pointer]:
            result.append(left_li[left_pointer])
            left_pointer += 1
        else:
            result.append(right_li[right_pointer])
            right_pointer += 1
    result += left_li[left_pointer:]
    result += right_li[right_pointer:]
    return result
li = [1,4,8,7,3,6,2,5]
print(li)
sorted_li = merge_sort(li)
print(li)
print(sorted_li)
[1, 4, 8, 7, 3, 6, 2, 5]
[1, 4, 8, 7, 3, 6, 2, 5]
[1, 2, 3, 4, 5, 6, 7, 8]