import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
#显示中文字体
mpl.rcParams["font.sans-serif"]=["SimHei"]#设置正常显示字符
mpl.rcParams["axes.unicode_minus"]=False
一、 numpy初见
1.1 ndarray的介绍
# hello numpy
x=np.array([[1,2,3],[9,8,7],[6,5,4]])
x
array([[1, 2, 3],
[9, 8, 7],
[6, 5, 4]])
1.2 ndarray和原生python效率对比
import random
import time
import numpy as np
a =[]for i inrange(10000000):
a.append(random.random())# 通过%time魔法方法,查看当前代码运行一次所花费的时间%time sum_p =sum(a)#将python中的list转化为ndarray
b = np.array(a)%time sum_n = np.sum(b)