Python Homework(2018-05-23,第十二州週週三)

huanghh29發表於2018-05-29

Matplotlib

EX

Exercise 11.1
原始碼
import matplotlib as mpl
import matplotlib.pyplot as plt
import math
import numpy as np
x = np.arange(0,2,0.02)
plt.plot(x,np.sin((x-2)*(math.e**(-x*x))))
plt.title("Plotting a function")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.show()


影象


Exercise 11.2

原始碼

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FuncFormatter 

X = np.random.rand(20,10)

b = np.random.rand(10,1)

z = np.random.randn(20,1)

y = np.dot(X,b)+z

#最小二乘法估計b的值
b_,temp1,temp2,temp3= np.linalg.lstsq(X,y,rcond=None)

plt.title("Parameter plot")
plt.xlabel("index")
plt.ylabel("value")

#下標
xlabel = np.array(range(10))

#散點圖
p1 = plt.scatter(xlabel,b,s=20,c=(1,0,0),marker = 'o')
p2 = plt.scatter(xlabel,b_,s=20,c=(0,1,0),marker = 'x')

#畫出圖例
plt.legend([p1,p2],['True coefficients','Estimated coefficients'],loc = 'upper right')

plt.show()

影象

Exercise 11.3

原始碼

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

#標準正態分佈
mu, sigma = 0, 1
s = np.random.normal(loc=mu, scale=sigma, size=1000)

count, bins, _ = plt.hist(s, 25, density=True,color = 'b')

#高斯和密度估計
plt.plot(bins, 1./(np.sqrt(2*np.pi)*sigma)*np.exp(-(bins-mu)**2/(2*sigma**2)), lw=2, c='r')

plt.title("Histogram")
plt.show()

影象