强大的数据结构和python扩展库2

该系列为南京大学课程《用Python玩转数据》学习笔记,主要以思维导图的记录

4.4 扩展库SciPy

4_4

4.5 ndarray

4_5

讨论

NumPy中的通用函数与math库中函数的比较:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import time
import math
import numpy as np

x = np.random.randint(100, size=(1000, 1000))
m_sum = 0
t_m1 = time.process_time()
for row in x:
m_sum += math.fsum(row)
t_m2 = time.process_time()
print(m_sum)
t_n1 = time.process_time()
n_sum = x.sum()
t_n2 = time.process_time()
print(n_sum)
print('Running time of math:', t_m2 - t_m1)
print('Running time of numpy:',t_n2 - t_n1)

比较一个1000*1000的矩阵求和,输出结果为:

1
2
Running time of math: 0.059509000000000034
Running time of numpy: 0.0006780000000000674

相差两个数量级