置信橢圓原理以及橢圓圖形繪制方式
置信橢圓原理及橢圓圖形繪制
置信橢圓長短軸計(jì)算

def confidence_oval(self,factor, ppf_rate):
pca1_std = np.std(factor.iloc[:, 0])
pca2_std = np.std(factor.iloc[:, 1])
f_value = scipy.stats.f.ppf(ppf_rate, dfn=2, dfd=factor.iloc[:, 0].shape[0] - 2)
x_axis = np.sqrt(
pca1_std ** 2 * f_value * 2 * ((factor.iloc[:, 0].shape[0] - 1) / (factor.iloc[:, 0].shape[0] - 2)))
y_axis = np.sqrt(
pca2_std ** 2 * f_value * 2 * ((factor.iloc[:, 0].shape[0] - 1) / (factor.iloc[:, 0].shape[0] - 2)))
x_axis = '%.2f' % x_axis
y_axis = '%.2f' % y_axis
return x_axis, y_axis
Python圖形繪制
def elli_plot(self,full_data, ellipse, y):
'''
:param full_data: pls后的點(diǎn)
:param ellipse: [橢圓長軸,橢圓短軸]
:param y:
:return:
'''
fig = plt.figure(figsize=(15, 5))
ax = fig.add_subplot(111)
elli = Ellipse(xy=(0, 0), width=float(ellipse[0]) * 2, height=float(ellipse[1]) * 2)
ax.add_patch(elli)
# 偏厚
outlier_data = y.loc[y[y.columns[0]] == 3, :]
# 偏薄
outlier_data_less = y.loc[y[y.columns[0]] == 1, :]
inner_data = full_data['pls']['pls'].loc[full_data['pls']['pls'].index.isin(outlier_data.index.tolist()+outlier_data_less.index.tolist()) == False, :]
ax.plot(outlier_data.iloc[:, 0], outlier_data.iloc[:, 1], 'ro')
ax.plot(outlier_data_less.iloc[:, 0], outlier_data_less.iloc[:, 1], 'bo')
ax.plot(inner_data.iloc[:, 0], inner_data.iloc[:, 1], 'yo')
name = str(self.picture_id)
plt.savefig("E:\\shhl\\1118_兩次PLS\\偏厚\\圖\\"+name+".png")
self.picture_id = self.picture_id +1
plt.show()
from matplotlib.patches import Ellipse, Circle
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ell1 = Ellipse(xy = (0.0, 0.0), width = 4, height = 8, angle = 30.0, facecolor= 'yellow', alpha=0.3)
cir1 = Circle(xy = (0.0, 0.0), radius=2, alpha=0.5)
ax.add_patch(ell1)
ax.add_patch(cir1)
x, y = 0, 0
ax.scatter([0,1], [0,1],color='red')
ax.scatter([2,1], [1,1],color='green')
plt.axis('scaled')
plt.axis('equal') #changes limits of x or y axis so that equal increments of x and y have the same length
plt.show()
置信橢圓-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) #計(jì)算協(xié)方差矩陣 lambda_, v = np.linalg.eig(cov) # 計(jì)算矩陣特征向量 lambda_ = np.sqrt(lambda_) s=4.605 #根據(jù)置信區(qū)間查卡方概率表 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()

總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)隨機(jī)分層抽樣的示例詳解
在數(shù)據(jù)分析與機(jī)器學(xué)習(xí)的實(shí)踐中,抽樣是不可或缺的一步,分層抽樣作為一種常用的抽樣方法,能夠確保樣本在不同類別中的比例與總體一致,下面我們看看如何使用Python實(shí)現(xiàn)隨機(jī)分層抽樣吧2024-11-11
Python簡單實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字的互相轉(zhuǎn)換功能示例
這篇文章主要介紹了Python簡單實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字的互相轉(zhuǎn)換功能,涉及Python針對字符串與列表的遍歷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
Python Opencv實(shí)戰(zhàn)之文字檢測OCR
這篇文章主要為大家詳細(xì)介紹了如何利用Python Opencv實(shí)現(xiàn)文字檢測OCR功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-08-08
typing.Dict和Dict的區(qū)別及它們在Python中的用途小結(jié)
當(dāng)在 Python 函數(shù)中聲明一個 dictionary 作為參數(shù)時(shí),我們一般會把 key 和 value 的數(shù)據(jù)類型聲明為全局變量,而不是局部變量。,這篇文章主要介紹了typing.Dict和Dict的區(qū)別及它們在Python中的用途小結(jié),需要的朋友可以參考下2023-06-06
python實(shí)現(xiàn)合并多個list及合并多個django QuerySet的方法示例
這篇文章主要介紹了python實(shí)現(xiàn)合并多個list及合并多個django QuerySet的方法,結(jié)合實(shí)例形式分析了Python使用chain合并多個list以及合并Django中多個QuerySet的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
Python matplotlib繪圖可視化知識點(diǎn)整理(小結(jié))
這篇文章主要介紹了Python matplotlib繪圖可視化知識點(diǎn)整理(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Python 中 Selenium 的 getAttribute()
本文將解釋如何使用Selenium的getAttribute()方法,getAttribute() 方法可以檢索元素屬性,例如錨標(biāo)記的 href 屬性, 該函數(shù)最初將嘗試返回指定屬性的值,感興趣的朋友跟隨小編一起看看吧2023-11-11

