Python中利用guiqwt進(jìn)行曲線數(shù)據(jù)擬合。

創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,包括成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營銷策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營銷等。創(chuàng)新互聯(lián)為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)核心團(tuán)隊(duì)十多年專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。
示例程序:
圖形界面如下:
# 自定義繪制ks曲線的函數(shù)
def plot_ks(y_test, y_score, positive_flag):
# 對(duì)y_test,y_score重新設(shè)置索引
y_test.index = np.arange(len(y_test))
#y_score.index = np.arange(len(y_score))
# 構(gòu)建目標(biāo)數(shù)據(jù)集
target_data = pd.DataFrame({'y_test':y_test, 'y_score':y_score})
# 按y_score降序排列
target_data.sort_values(by = 'y_score', ascending = False, inplace = True)
# 自定義分位點(diǎn)
cuts = np.arange(0.1,1,0.1)
# 計(jì)算各分位點(diǎn)對(duì)應(yīng)的Score值
index = len(target_data.y_score)*cuts
scores = target_data.y_score.iloc[index.astype('int')]
# 根據(jù)不同的Score值,計(jì)算Sensitivity和Specificity
Sensitivity = []
Specificity = []
for score in scores:
? ? # 正例覆蓋樣本數(shù)量與實(shí)際正例樣本量
? ? positive_recall = target_data.loc[(target_data.y_test == positive_flag) (target_data.y_scorescore),:].shape[0]
? ? positive = sum(target_data.y_test == positive_flag)
? ? # 負(fù)例覆蓋樣本數(shù)量與實(shí)際負(fù)例樣本量
? ? negative_recall = target_data.loc[(target_data.y_test != positive_flag) (target_data.y_score=score),:].shape[0]
? ? negative = sum(target_data.y_test != positive_flag)
? ? Sensitivity.append(positive_recall/positive)
? ? Specificity.append(negative_recall/negative)
# 構(gòu)建繪圖數(shù)據(jù)
plot_data = pd.DataFrame({'cuts':cuts,'y1':1-np.array(Specificity),'y2':np.array(Sensitivity),
? ? ? ? ? ? ? ? ? ? ? ? ? 'ks':np.array(Sensitivity)-(1-np.array(Specificity))})
# 尋找Sensitivity和1-Specificity之差的最大值索引
max_ks_index = np.argmax(plot_data.ks)
plt.plot([0]+cuts.tolist()+[1], [0]+plot_data.y1.tolist()+[1], label = '1-Specificity')
plt.plot([0]+cuts.tolist()+[1], [0]+plot_data.y2.tolist()+[1], label = 'Sensitivity')
# 添加參考線
plt.vlines(plot_data.cuts[max_ks_index], ymin = plot_data.y1[max_ks_index],
? ? ? ? ? ymax = plot_data.y2[max_ks_index], linestyles = '--')
# 添加文本信息
plt.text(x = plot_data.cuts[max_ks_index]+0.01,
? ? ? ? y = plot_data.y1[max_ks_index]+plot_data.ks[max_ks_index]/2,
? ? ? ? s = 'KS= %.2f' %plot_data.ks[max_ks_index])
# 顯示圖例
plt.legend()
# 顯示圖形
plt.show()
# 調(diào)用自定義函數(shù),繪制K-S曲線
plot_ks(y_test = y_test, y_score = y_score, positive_flag = 1)
輸入以下代碼導(dǎo)入我們用到的函數(shù)庫。
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(0,5,0.1);
y=np.sin(x);
plt.plot(x,y)
采用剛才代碼后有可能無法顯示下圖,然后在輸入以下代碼就可以了:
plt.show()
名稱欄目:Python提取曲線函數(shù),python提取曲線數(shù)據(jù)線
URL分享:http://chinadenli.net/article34/hshpse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站制作、定制網(wǎng)站、建站公司、動(dòng)態(tài)網(wǎng)站、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容