[高级编程技术作业-Week 12]Matplotlib相关习题练习

Codes and Results:

#11.1

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2, 100)
y = (np.sin(x - 2))**2 * np.exp(-x*x)
plt.plot(x, y, 'g-', label = 'f(x)')            #scatter plot
#plt.title('f(x) = (sin(x-2))^2 * e^(-x^2)')
plt.title('$f(x) = sin^2(x-2) * e^{-x^2}$')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()

plt.show()
#11.2
import matplotlib.pyplot as plt
import numpy as np

var = np.arange(0,10)                 # 10 variables
X = np.random.random((20,10)) * 10    # observations of var
b = np.random.random((10,))*2-1       # [0,1) --> [-1,1)
z = np.random.normal(size=20)

y = X.dot(b) + z
b_esti = np.linalg.lstsq(X, y, rcond=-1)[0]    #least squares

f, sp = plt.subplots()
sp.plot(var, b, 'rx', label = 'True coefficients')
sp.plot(var, b_esti, 'bo', label = 'Estimated coefficients')     #scatter plot
plt.xlim((-1, 10))
plt.ylim((-1.5, 1.5))
plt.xlabel('index')
plt.ylabel('value')
plt.legend()

plt.show()

#11.3

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats

z = np.random.normal(loc = 0., scale = 1., size = 10000)
x = np.linspace(np.min(z), np.max(z), 10000)
kernel = scipy.stats.gaussian_kde(z)          # Gaussian kernel density estimator
f, sp = plt.subplots()
sp.hist(z, bins = 25, density = True)         # histogram
sp.plot(x, kernel.pdf(x)) 
plt.title('PDF for N(0,1)')
plt.xlabel('x')
plt.ylabel('y')
plt.show()



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值