置信椭圆-python

卡方概率表:https://people.richland.edu/james/lecture/m170/tbl-chi.html
opencv画椭圆:https://docs.opencv.org/2.4.9/modules/core/doc/drawing_functions.html?highlight=ellipse#cv2.ellipse
numpy.linalg.eig() 特征向量求解矩阵:https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.linalg.eig.html

cov = np.cov(x, y) #计算协方差矩阵
lambda_, v = np.linalg.eig(cov) # 计算矩阵特征向量
lambda_ = np.sqrt(lambda_)
s=4.605 #根据置信区间查卡方概率表 95% 5.991 99% 9.21 90% 4.605
ax = plt.subplot(111, aspect=‘equal’)

ell = Ellipse(xy=(np.mean(x), np.mean(y)),
width=lambda_[0]*np.sqrt(s) *2, height=lambda_[1]*np.sqrt(s)*2,
angle=np.rad2deg(np.arccos(v[0, 0])),facecolor=‘yellow’,alpha=0.3)

ax.add_artist(ell)
plt.scatter(x, y)
plt.axis(‘scaled’)
plt.axis(‘equal’)
plt.show()

置信椭圆-python_第1张图片

你可能感兴趣的:(主成分分析)