赞
踩
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
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
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()
结果如下:
# 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()
结果如下:
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()
结果如下:
#注意: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()
结果如下:
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()
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()
结果如下:
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()
结果如下:
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()
结果如下:
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()
结果如下:
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()
结果如下:
重心插值:
(1)给定实数对,xj互不相同。
(2)对于给定的n+1个权值,有:
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()
结果如下:
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=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'])]
结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。