Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

9.7. NumPy

NumPy is the fundamental package for scientific computing with Python.

9.7.1. 随机数

9.7.1.1. 随机种子

np.random.seed(1) 两次产生的随机数相同

			
import numpy as np
np.random.seed(1)

L1 = np.random.randn(3, 3)
L2 = np.random.randn(3, 3)
print(L1)
print(L2)
			
			
			
[[-1.32959475 -0.35726593 -1.01824748]
 [-0.16875459 -1.40799966 -0.42432159]
 [-2.90363742  0.77847352 -0.03868502]]
[[ 0.8140817   0.47350462 -0.44832424]
 [-0.21463067  0.18678995 -1.56375306]
 [ 0.39748247 -0.74620674 -0.97122838]]	
			
			

9.7.1.2. 随机矩阵数组

生成随机浮点矩阵数组

		
import numpy as np
number = np.random.rand(10, 5)
print(number)		
		
			
		
neo@MacBook-Pro-Neo ~/workspace/python % python3.9 /Users/neo/workspace/python/numpy/test.py
[[0.07210811 0.89871612 0.31670349 0.88870892 0.38252093]
 [0.08210199 0.37878429 0.09693934 0.53084051 0.81222326]
 [0.99527501 0.39815405 0.02937093 0.21271075 0.09775669]
 [0.97038382 0.10373132 0.60815363 0.00740848 0.51247618]
 [0.77290466 0.7961732  0.21776523 0.27498686 0.84316289]
 [0.11457979 0.98606765 0.36357378 0.00754072 0.62702464]
 [0.19330684 0.60832298 0.57052479 0.81215836 0.04167786]
 [0.71456373 0.9203253  0.27650414 0.6247527  0.28517774]
 [0.85126634 0.06420073 0.92123025 0.84654969 0.11828913]
 [0.38481704 0.95317434 0.62498057 0.5297113  0.22969415]]		
		
			
		
pd.DataFrame(np.random.rand(10,2))		
		
			

9.7.1.3. 生成随机整数矩阵数组

np.random.randint(从 0 ,到 255,(5,6,3))

			
import numpy as np

np.random.seed(1)
x = np.random.randint(0,255,(5,6,3))
print(x)			
			
			
			
[[[ 37 235 140]
  [ 72 137 203]
  [133  79 192]
  [144 129 204]
  [ 71 237 252]
  [134  25 178]]

 [[ 20 254 101]
  [146 212 139]
  [252 234 156]
  [157 142  50]
  [ 68 215 215]
  [233 241 247]]

 [[222  96  86]
  [141 233 137]
  [  7  63  61]
  [ 22  57   1]
  [128  60 209]
  [  8 216 141]]

 [[115 175 234]
  [121 200  30]
  [ 71 131 198]
  [149  49  57]
  [  3 196  24]
  [241  43  76]]

 [[ 26  52  80]
  [109 115  41]
  [210  15  64]
  [196  25 111]
  [226 215 135]
  [ 26 153 104]]]			
			
			

9.7.2. 生成数列

从0 到 100,间隔为10的数值序列

		
n = np.linspace(start = 0, stop = 100, num = 11)
		
x = np.linspace(-3, 3, 50)		
		
		

9.7.3. 查看矩阵或者数组的维度

		
import numpy as np

np.random.seed(1)
matrix = np.random.randint(0,255,(5,6,3))
print(matrix.shape)		
		
		
		
(5, 6, 3)		
		
		

9.7.3.1. 数组维度

			
from numpy import array
a = array([[1,1],[1,2],[1,3],[1,4]])
print(a.shape)			
			
			
			
(4, 2)
			
			

9.7.4. 数据类型

9.7.4.1. 显示数据类型

			
import numpy

numpy.random.seed(1)
matrix = numpy.random.randint(0,255,(5,6,3))
print(matrix.dtype)		
			
			

9.7.4.2. 转换数据类型

			
import numpy

numpy.random.seed(1)
matrix = numpy.random.randint(0,255,(5,6,3))
print(matrix.dtype)

matrix1 = matrix.astype(np.int8)
print(matrix1.dtype)			
			
			

9.7.5. max

		
# 创建一个包含学生成绩的二维数组
data = np.array([
    ['Alice', 85, 92, 78],
    ['Bob', 89, 90, 95],
    ['Charlie', 91, 85, 88],
    ['David', 78, 80, 82]
])

# 提取成绩列(忽略姓名列)
grades = data[:, 1:].astype(int)
print(grades)

# 计算每位学生的最高成绩
max_grades = np.max(grades, axis=1)
print(max_grades)

# 打印结果
for i, student in enumerate(data[:, 0]):
    print(f"{student} 的最好成绩是: {max_grades[i]}")		
		
		

9.7.6. max

		
import numpy as np

help(np.amax)

a = np.arange(9).reshape((3, 3))
max_all = np.amax(a)
max_dimension1 = np.amax(a, axis=0)
max_dimension2 = np.amax(a, axis=1)

print('a:\n', a)
print('max_all:', max_all)
print('max_dimension1:', max_dimension1)
print('max_dimension2:', max_dimension2)	
		
		
		
D:\workspace\netkiller\.venv\Scripts\python.exe D:\workspace\netkiller\test\test1.py 
Help on _ArrayFunctionDispatcher in module numpy:

amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)
    Return the maximum of an array or maximum along an axis.

    `amax` is an alias of `~numpy.max`.

    See Also
    --------
    max : alias of this function
    ndarray.max : equivalent method

a:
 [[0 1 2]
 [3 4 5]
 [6 7 8]]
max_all: 8
max_dimension1: [6 7 8]
max_dimension2: [2 5 8]		
		
		

9.7.7. 显示矩阵

		
import numpy
from matplotlib import pyplot as plt

numpy.random.seed(1)
matrix = numpy.random.randint(0,255,(5,6,3))
print(matrix.shape)
plt.imshow(matrix)