题目一:

 (十六)matplotlib

思路:构造一个表示该方程的函数,然后利用plot打印。

代码:

from cmath import *
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
import seaborn


def f(x):
    temp=-1*pow(x,2)
    return np.sin(x-2)*np.sin(x-2)*pow(e,temp)
a = np.arange(-1,3,0.02)
b=np.sin(a)
plt.plot(a,f(a),'r--')
plt.axis([0,2,0,2])
plt.show()

 

运行结果:

 (十六)matplotlib

 

 

题目二:

 (十六)matplotlib

思路:利用numpy.lstsq来利用最小二乘法的方法求出系数向量b的估计值。

代码(import同题一):

n=20
m=10
b=np.random.randint(0,10,(m,1))#系数向量
print("b:")
print(b)
X=np.random.randint(0,10,(n,m))#数据矩阵
print("X:")
print(X)
n=np.random.randn(n,1)#(20,1)的正太列向量
print("n:")
print(n)
y=np.dot(X,b)+n
print("y=Xb+n:")
print(y)

bo=np.linalg.lstsq(X,y,rcond=None)[0]#最小的b系数的估计值
print("bo:")
print(bo)
plt.scatter(range(10),list(bo.T),marker='o')
plt.scatter(range(10),list(b.T),marker='x')
plt.legend(['True','Estimated'])
plt.show()

 

运行结果:

 (十六)matplotlib

 

题目三:

 (十六)matplotlib

思路:利用seaborn的绘图函数distplot,得到按照当前数据统计得到的数据分布直方图。

代码:

X=np.random.randn(10000)
seaborn.distplot(X,bins=25,kde=True,rug=True)
plt.show()

运行结果:

 (十六)matplotlib

 

相关文章:

  • 2021-10-16
  • 2022-12-23
  • 2021-06-18
  • 2022-12-23
  • 2021-12-08
  • 2021-10-11
  • 2021-04-04
  • 2021-05-31
猜你喜欢
  • 2022-12-23
  • 2022-01-02
  • 2022-02-25
  • 2021-08-30
  • 2021-04-23
  • 2021-09-02
  • 2021-10-28
相关资源
相似解决方案