python基本操作总结(二)
2017-11-01 11:32
387 查看
1 基本绘图
1.1 绘制正态分布概率密度函数
mu = 0 #均值 sigma = 1 #标准差 x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 50) #概率密度函数 y = np.exp(-(x - mu) ** 2 / (2 * sigma ** 2)) / (math.sqrt(2 * math.pi) * sigma) print x.shape print 'x = \n', x print y.shape print 'y = \n', y plt.plot(x, y, 'r-', x, y, 'go', linewidth=2, markersize=8) plt.grid(True) plt.show()
结果如下:
解决图表中文字符方法:
matplotlib.rcParams['font.sans-serif'] = [u'SimHei'] #FangSong/黑体 FangSong/KaiTi matplotlib.rcParams['axes.unicode_minus'] = False
1.2 损失函数:
Logistic损失(-1,1)/SVM Hinge损失/ 0-1损失x = np.array(np.linspace(start=-2, stop=3, num=1001, dtype=np.float)) # Logistic损失 y_logit = np.log(1 + np.exp(-x)) / math.log(2) # adboost损失 y_boost = np.exp(-x) # 0-1损失 y_01 = x < 0 # SVM Hinge损失 y_hinge = 1.0 - x y_hinge[y_hinge < 0] = 0 4000 plt.plot(x, y_logit, 'r-', label='Logistic Loss', linewidth=2) plt.plot(x, y_01, 'g-', label='0/1 Loss', linewidth=2) plt.plot(x, y_hinge, 'b-', label='Hinge Loss', linewidth=2) plt.plot(x, y_boost, 'm--', label='Adaboost Loss', linewidth=2) plt.grid() plt.legend(loc='upper right') # plt.savefig('1.png') plt.show()
结果如下:
1.3 x^x
# x ** x x > 0 # (-x) ** (-x) x < 0 def f(x): # 初始化函数关系 y = np.ones_like(x) i = x > 0 y[i] = np.power(x[i], x[i]) i = x < 0 y[i] = np.power(-x[i], -x[i]) return y x = np.linspace(-1.3, 1.3, 101) y = f(x) plt.plot(x, y, 'g-', label='x^x', linewidth=2) plt.grid() plt.legend(loc='upper left') plt.show()
结果如下:
1.4 胸型线
x = np.arange(1, 0, -0.001) #加入一个e^x4次幂的扰动 y = (-3 * x * np.log(x) + np.exp(-(40 * (x - 1 / np.e)) ** 4) / 25) / 2 plt.figure(figsize=(5,7)) plt.plot(y, x, 'r-', linewidth=2) plt.grid(True) plt.show()
结果如下:
1.5 心形线
#注意:0-7范围需要大于2π才能划出完整的心形线 t = np.linspace(0, 7, 100) x = 16 * np.sin(t) ** 3 y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t) plt.plot(x, y, 'r-', linewidth=2) plt.grid(True) plt.show()
结果如下:
1.6 渐开线
t = np.linspace(0, 50, num=1000) x = t*np.sin(t) + np.cos(t) y = np.sin(t) - t*np.cos(t) plt.plot(x, y, 'r-', linewidth=2) plt.grid() plt.show()
1.7 两种sinx
matplotlib.rcParams['font.sans-serif'] = [u'SimHei'] #黑体 FangSong/KaiTi matplotlib.rcParams['axes.unicode_minus'] = False x = np.arange(0, 10, 0.1) y = np.sin(x) plt.bar(x, y, width=0.04, linewidth=0.2) plt.plot(x, y, 'r--', linewidth=2) plt.title(u'Sin曲线') plt.xticks(rotation=-60) plt.xlabel('X') plt.ylabel('Y') plt.grid() plt.show()
结果如下:
2 概率分布
2.1 均匀分布
plt.style.use('classic') x = np.random.rand(10000) t = np.arange(len(x)) plt.hist(x, 30, color='m', alpha=0.5) #plt.plot(t, x, 'r-', label=u'均匀分布') plt.legend(loc='upper left') plt.grid() plt.show()
结果如下:
2.2 验证中心极限定理
t = 10000 a = np.zeros(1000) for i in range(t): a += np.random.uniform(-5, 5, 1000) a /= t plt.hist(a, bins=30, color='g', alpha=0.5, normed=True) plt.grid() plt.show()
结果如下:
2.3 Poisson分布
x = np.random.poisson(lam=5, size=10000) print x pillar = 15 a = plt.hist(x, bins=pillar, normed=True, range=[0, pillar], color='g', alpha=0.5) plt.grid() plt.show() print a print a[0].sum()
结果如下:
2.4 直方图的使用
mu = 2 sigma = 3 data = mu + sigma * np.random.randn(1000) h = plt.hist(data, 30, normed=1, color='#a0a0ff') x = h[1] #pdf概率密度函数 y = norm.pdf(x, loc=mu, scale=sigma) plt.plot(x, y, 'r--', x, y, 'ro', linewidth=2, markersize=4) plt.grid() plt.show()
结果如下:
2.5 插值
重心插值:(1)给定实数对
,xj互不相同。
(2)对于给定的n+1个权值
,有:
(3)则函数f(x)在xk处的值为yk。
对于权值,可以选择
对2.4中泊松分布进行重心插值和三次样条插值拟合后曲线。
rv = poisson(5) x1 = a[1] y1 = rv.pmf(x1) itp = BarycentricInterpolator(x1, y1) # 重心插值 x2 = np.linspace(x.min(), x.max(), 50) y2 = itp(x2) cs = scipy.interpolate.CubicSpline(x1, y1) # 三次样条插值 plt.plot(x2, cs(x2), 'm--', linewidth=5, label='CubicSpine') # 三次样条插值 plt.plot(x2, y2, 'g-', linewidth=3, label='BarycentricInterpolator') # 重心插值 plt.plot(x1, y1, 'r-', linewidth=1, label='Actural Value') # 原始值 plt.legend(loc='upper right') plt.grid() plt.show()
结果如下:
3 绘制三维图像
x, y = np.ogrid[-3:3:100j, -3:3:100j] u = np.linspace(-3, 3, 101) x, y = np.meshgrid(u, u) z = x*y*np.exp(-(x**2 + y**2)/2) / math.sqrt(2*math.pi) #标准的概率密度函数 # z = np.exp(-(x**2 + y**2)/2) / math.sqrt(2*math.pi) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(x, y, z, rstride= a506 5, cstride=5, cmap=cm.ocean, linewidth=0.1) #ax.plot_surface(x, y, z, rstride=5, cstride=5, cmap=cm.rainbow, linewidth=0.5) plt.show() # cmaps = [('Perceptually Uniform Sequential', # ['viridis', 'inferno', 'plasma', 'magma']), # ('Sequential', ['Blues', 'BuGn', 'BuPu', # 'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd', # 'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu', # 'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']), # ('Sequential (2)', ['afmhot', 'autumn', 'bone', 'cool', # 'copper', 'gist_heat', 'gray', 'hot', # 'pink', 'spring', 'summer', 'winter']), # ('Diverging', ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr', # 'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral', # 'seismic']), # ('Qualitative', ['Accent', 'Dark2', 'Paired', 'Pastel1', # 'Pastel2', 'Set1', 'Set2', 'Set3']), # ('Miscellaneous', ['gist_earth', 'terrain', 'ocean', 'gist_stern', # 'brg', 'CMRmap', 'cubehelix', # 'gnuplot', 'gnuplot2', 'gist_ncar', # 'nipy_spectral', 'jet', 'rainbow', # 'gist_rainbow', 'hsv', 'flag', 'prism'])]
结果如下:
相关文章推荐
- Python 操作文件的基本方法总结
- python基本操作总结(一) e037
- Python文件与文件夹常见基本操作总结
- Python基础-基本操作命令总结
- Python文件与文件夹常见基本操作总结
- python学习总结 基本操作
- Python:文件操作总结1——文件基本操作
- python字典基本操作总结
- python基本操作总结(三)
- python文件相关操作总结
- 菜鸟学python(11)list列表基本操作
- MATLAB复杂网络工具箱——matgraph基本操作总结
- python开发 Mysqldb安装及基本操作
- python函数基本操作
- Python学习总结笔记(9)-- MySQL数据库操作之mysql-connector
- Python图像处理的基本操作(一)
- python基本知识、数据库、网络、编程等总结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- 自己总结的sql基本操作
- oracle基本操作语句总结