【Python】第六章(numpy)综合练习

【Python】第六章(numpy)综合练习_第1张图片

import numpy as np
# 1:
arr = np.array(range(32)).reshape(8, 4)
print(arr)

# 2:
arr2 = np.linspace(1, 2, 3)
print(arr2)

# 3:
arr3 = np.identity(9)  # 9*9的方阵
arr31 = np.eye(9)
print(arr3)

# 4:
arr4 = np.zeros(3)
print(arr4)

# 5:
arr5 = np.random.randint(1, 10, (2, 2))
print(arr5)

 【Python】第六章(numpy)综合练习_第2张图片

import numpy as np
arr = np.arange(10)
# 1:
print(arr[1], arr[2])

# 2:
arr[4:7] = 12
print(arr)

# 3:
arr[5:] = 10
print(arr)

【Python】第六章(numpy)综合练习_第3张图片

import numpy as np
arr = np.arange(1, 10).reshape(3, 3)
# 1:
print(arr[0])
# 2:
print(arr[1:])

 【Python】第六章(numpy)综合练习_第4张图片

import numpy as np
arr = np.array([4, 5, 6])
# 1:
print(type(arr))

# 2:
print(arr.shape)

# 3:
print(arr[0])

 【Python】第六章(numpy)综合练习_第5张图片

import numpy as np
b = np.array([[4, 5, 6], [1, 2, 3]])
# 1:
print(b.shape)

# 2:
print(b[0][0], b[0][1], b[1][1])
print(b[0, 0], b[0, 1], b[1, 1])

 【Python】第六章(numpy)综合练习_第6张图片

import numpy as np
# 1:
a = np.zeros((3, 3), dtype=int)
# 2:
b = np.ones((4, 5))
# 3:
c = np.eye(4)
# 4:
d = np.random.rand(3, 2)


print(a)
print(b)
print(c)
print(d)

【Python】第六章(numpy)综合练习_第7张图片

import numpy as np
a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
b = a[0:2, 2:4]

# 1:
print(b[0, 0])

# 2:
print(a[0, -1])
print(b[0, -1])

import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print(a[0,0],a[1,1],a[2,0])

【Python】第六章(numpy)综合练习_第8张图片

import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(a[0,0],a[1,2],a[2,0],a[3,1])

[ 1,  6,  7, 11]

 【Python】第六章(numpy)综合练习_第9张图片

 【Python】第六章(numpy)综合练习_第10张图片

 【Python】第六章(numpy)综合练习_第11张图片

 【Python】第六章(numpy)综合练习_第12张图片

 【Python】第六章(numpy)综合练习_第13张图片

 【Python】第六章(numpy)综合练习_第14张图片

 【Python】第六章(numpy)综合练习_第15张图片

10.0
[4. 6.]
[3. 7.]
2.5
[2. 3.]
[1.5 3.5]

 【Python】第六章(numpy)综合练习_第16张图片

[[1 2 5 6]
 [3 4 7 8]]
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
[[1 2]
 [3 8]]
[[ 1  2]
 [ 3 12]] 

【Python】第六章(numpy)综合练习_第17张图片

 【Python】第六章(numpy)综合练习_第18张图片

nan

False

False

nan

False

 

import numpy as np
obj1 = np.zeros(10)
obj1[4] = 1
print(obj1)

 

import numpy as np
obj1 = np.array(range(10, 50))
print(obj1[::-1])

【Python】第六章(numpy)综合练习_第19张图片

import numpy as np
obj1 = np.random.random((10, 10))
print(np.max(obj1), np.min(obj1))
print(np.sort(obj1))

 

import numpy as np
obj1 = np.zeros((10, 10), dtype=int)
obj1[0] = 1
obj1[-1] = 1
obj1[:, 0] = 1
obj1[:, -1] = 1
print(obj1)

【Python】第六章(numpy)综合练习_第20张图片

 【Python】第六章(numpy)综合练习_第21张图片

import numpy as np
# 1:
obj1 = np.array([range(5)]*5)
print(obj1)
# 2:
obj2 = np.linspace(0, 1, 12)
print(obj2)
# 3:
obj3 = np.random.rand(10)
print(np.sort(obj3))
# 4:
"""
np.argwhere(条件)->好像列表推导式
只用np.argmax()只能返回第一个下标
"""
arr = np.random.randint(1,10,10)
all_index_max = np.argwhere(arr == np.max(arr)).reshape(-1)  # 通过reshape(-1)转置
arr[all_index_max] = 0
print(arr)

 

import numpy as np
arr = np.random.randint(0, 100, (5, 5))
print(arr)
key = arr[:, 2]
print(np.argsort(key))
print(arr[np.argsort(key)])

import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.zeros(17, dtype=int)  # 3*4+5=17
b[::4] = a
print(b)

 【Python】第六章(numpy)综合练习_第22张图片

import numpy as np
m = np.random.randint(0, 5, (5, 5))
print(m)
m[[1, 2]] = m[[2, 1]]  # 交换第2行和第3行
print(m)

 

import numpy as np
p = np.random.randint(0, 5, size=(5, 4))
all_mean = np.mean(p, axis=1).reshape(5, 1)
print(p)
print(p-all_mean)

 【Python】第六章(numpy)综合练习_第23张图片

import numpy as np
x = np.zeros((8, 8), dtype=int)
x[1::2, ::2] = 1
x[::2, 1::2] = 1
print(x)

【Python】第六章(numpy)综合练习_第24张图片

import numpy as np
x = np.random.rand(5, 5)
max_x = np.max(x)
min_x = np.min(x)
print(max_x, min_x)
print((x-max_x)/(max_x-min_x))

【Python】第六章(numpy)综合练习_第25张图片

import numpy as np
# 1:
a = np.random.randn(10)
print(np.where(a > 0, 1, -1))

# 2:
x = np.array([[0,7,9,5,8,1,2,6,0,4]])
print(np.piecewise(x, [x<3, ((x>3)&(x<5)), x>7], [-1, 1, lambda x:x*4]))

【Python】第六章(numpy)综合练习_第26张图片

a1:

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

b1:

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

a2:
[ 0  0  0  0  4  5  6  7  8  9 10 11]

b2:

[[ 0  0  0  0]
 [ 4  5  6  7]
 [ 8  9 10 11]]

a3:
[ 0  1  2  3  4  5  6  7  8  9 10 11]

b3:

[[ 0  0  0  0]
 [ 4  5  6  7]
 [ 8  9 10 11]]

【Python】第六章(numpy)综合练习_第27张图片

import numpy as np
x = np.array([[0,1,2],[3,4,5],[6,7,8]])

b = np.append(x,[[7,8,9]],axis=0)      # 插入一行
c = np.append(x,[[7],[8],[9]],axis=1)  # 插入一列

print(b)
print(c)

你可能感兴趣的:(python,python)