最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python機器學(xué)習(xí)應(yīng)用之基于BP神經(jīng)網(wǎng)絡(luò)的預(yù)測篇詳解

 更新時間:2022年01月18日 17:08:53   作者:柚子味的羊  
BP(back?propagation)神經(jīng)網(wǎng)絡(luò)是1986年由Rumelhart和McClelland為首的科學(xué)家提出的概念,是一種按照誤差逆向傳播算法訓(xùn)練的多層前饋神經(jīng)網(wǎng)絡(luò),是應(yīng)用最廣泛的神經(jīng)網(wǎng)絡(luò)模型之一

一、Introduction

1 BP神經(jīng)網(wǎng)絡(luò)的優(yōu)點

  • 非線性映射能力:BP神經(jīng)網(wǎng)絡(luò)實質(zhì)上實現(xiàn)了一個從輸入到輸出的映射功能,數(shù)學(xué)理論證明三層的神經(jīng)網(wǎng)絡(luò)就能夠以任意精度逼近任何非線性連續(xù)函數(shù)。這使得其特別適合于求解內(nèi)部機制復(fù)雜的問題,即BP神經(jīng)網(wǎng)絡(luò)具有較強的非線性映射能力。
  • 自學(xué)習(xí)和自適應(yīng)能力:BP神經(jīng)網(wǎng)絡(luò)在訓(xùn)練時,能夠通過學(xué)習(xí)自動提取輸入、輸出數(shù)據(jù)間的“合理規(guī)則”,并自適應(yīng)地將學(xué)習(xí)內(nèi)容記憶于網(wǎng)絡(luò)的權(quán)值中。即BP神經(jīng)網(wǎng)絡(luò)具有高度自學(xué)習(xí)和自適應(yīng)的能力。
  • 泛化能力:所謂泛化能力是指在設(shè)計模式分類器時,即要考慮網(wǎng)絡(luò)在保證對所需分類對象進行正確分類,還要關(guān)心網(wǎng)絡(luò)在經(jīng)過訓(xùn)練后,能否對未見過的模式或有噪聲污染的模式,進行正確的分類。也即BP神經(jīng)網(wǎng)絡(luò)具有將學(xué)習(xí)成果應(yīng)用于新知識的能力。

2 BP神經(jīng)網(wǎng)絡(luò)的缺點

  • 局部極小化問題:從數(shù)學(xué)角度看,傳統(tǒng)的 BP神經(jīng)網(wǎng)絡(luò)為一種局部搜索的優(yōu)化方法,它要解決的是一個復(fù)雜非線性化問題,網(wǎng)絡(luò)的權(quán)值是通過沿局部改善的方向逐漸進行調(diào)整的,這樣會使算法陷入局部極值,權(quán)值收斂到局部極小點,從而導(dǎo)致網(wǎng)絡(luò)訓(xùn)練失敗。加上BP神經(jīng)網(wǎng)絡(luò)對初始網(wǎng)絡(luò)權(quán)重非常敏感,以不同的權(quán)重初始化網(wǎng)絡(luò),其往往會收斂于不同的局部極小,這也是每次訓(xùn)練得到不同結(jié)果的根本原因
  • BP 神經(jīng)網(wǎng)絡(luò)算法的收斂速度慢:由于BP神經(jīng)網(wǎng)絡(luò)算法本質(zhì)上為梯度下降法,它所要優(yōu)化的目標函數(shù)是非常復(fù)雜的,因此,必然會出現(xiàn)“鋸齒形現(xiàn)象”,這使得BP算法低效;又由于優(yōu)化的目標函數(shù)很復(fù)雜,它必然會在神經(jīng)元輸出接近0或1的情況下,出現(xiàn)一些平坦區(qū),在這些區(qū)域內(nèi),權(quán)值誤差改變很小,使訓(xùn)練過程幾乎停頓;BP神經(jīng)網(wǎng)絡(luò)模型中,為了使網(wǎng)絡(luò)執(zhí)行BP算法,不能使用傳統(tǒng)的一維搜索法求每次迭代的步長,而必須把步長的更新規(guī)則預(yù)先賦予網(wǎng)絡(luò),這種方法也會引起算法低效。以上種種,導(dǎo)致了BP神經(jīng)網(wǎng)絡(luò)算法收斂速度慢的現(xiàn)象。
  • BP 神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)選擇不一:BP神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)的選擇至今尚無一種統(tǒng)一而完整的理論指導(dǎo),一般只能由經(jīng)驗選定。網(wǎng)絡(luò)結(jié)構(gòu)選擇過大,訓(xùn)練中效率不高,可能出現(xiàn)過擬合現(xiàn)象,造成網(wǎng)絡(luò)性能低,容錯性下降,若選擇過小,則又會造成網(wǎng)絡(luò)可能不收斂。而網(wǎng)絡(luò)的結(jié)構(gòu)直接影響網(wǎng)絡(luò)的逼近能力及推廣性質(zhì)。因此,應(yīng)用中如何選擇合適的網(wǎng)絡(luò)結(jié)構(gòu)是一個重要的問題。

二、實現(xiàn)過程

1 Demo

#%% 基礎(chǔ)數(shù)組運算庫導(dǎo)入
import numpy as np 
# 畫圖庫導(dǎo)入
import matplotlib.pyplot as plt 
# 導(dǎo)入三維顯示工具
from mpl_toolkits.mplot3d import Axes3D
# 導(dǎo)入BP模型
from sklearn.neural_network import MLPClassifier
# 導(dǎo)入demo數(shù)據(jù)制作方法
from sklearn.datasets import make_classification
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
import warnings
from sklearn.exceptions import ConvergenceWarning

#%%模型訓(xùn)練
# 制作五個類別的數(shù)據(jù),每個類別1000個樣本
train_samples, train_labels = make_classification(n_samples=1000, n_features=3, 
                           n_redundant=0,n_classes=5, n_informative=3, 
                           n_clusters_per_class=1,class_sep=3, random_state=10)
# 將五個類別的數(shù)據(jù)進行三維顯示
fig = plt.figure()
ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=20, azim=20)
ax.scatter(train_samples[:, 0], train_samples[:, 1], train_samples[:, 2], marker='o', c=train_labels)
plt.title('Demo Data Map')

#%% 建立 BP 模型, 采用sgd優(yōu)化器,relu非線性映射函數(shù)
BP = MLPClassifier(solver='sgd',activation = 'relu',max_iter = 500,alpha = 1e-3,
                   hidden_layer_sizes = (32,32),random_state = 1)
# 進行模型訓(xùn)練
with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=ConvergenceWarning,
                            module="sklearn")
    BP.fit(train_samples, train_labels)
# 查看 BP 模型的參數(shù)
print(BP)
#%% 進行模型預(yù)測
predict_labels = BP.predict(train_samples)
# 顯示預(yù)測的散點圖
fig = plt.figure()
ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=20, azim=20)
ax.scatter(train_samples[:, 0], train_samples[:, 1], train_samples[:, 2], marker='o', c=predict_labels)
plt.title('Demo Data Predict Map with BP Model')

# 顯示預(yù)測分數(shù)
print("預(yù)測準確率: {:.4f}".format(BP.score(train_samples, train_labels)))

# 可視化預(yù)測數(shù)據(jù) 
print("真實類別:", train_labels[:10])
print("預(yù)測類別:", predict_labels[:10])
# 準確率等報表
print(classification_report(train_labels, predict_labels))

# 計算混淆矩陣
classes = [0, 1, 2, 3]
cofusion_mat = confusion_matrix(train_labels, predict_labels, classes) 
sns.set()
figur, ax = plt.subplots()
# 畫熱力圖
sns.heatmap(cofusion_mat, cmap="YlGnBu_r", annot=True, ax=ax) 
ax.set_title('confusion matrix')  # 標題
ax.set_xticklabels([''] + classes, minor=True)
ax.set_yticklabels([''] + classes, minor=True)
ax.set_xlabel('predict')  # x軸
ax.set_ylabel('true')  # y軸
plt.show()

#%%# 進行新的測試數(shù)據(jù)測試
test_sample = np.array([[-1, 0.1, 0.1]])
print(f"{test_sample} 類別是: ", BP.predict(test_sample))
print(f"{test_sample} 類別概率分別是: ", BP.predict_proba(test_sample))

test_sample = np.array([[-1.2, 10, -91]])
print(f"{test_sample} 類別是: ", BP.predict(test_sample))
print(f"{test_sample} 類別概率分別是: ", BP.predict_proba(test_sample))

test_sample = np.array([[-12, -0.1, -0.1]])
print(f"{test_sample} 類別是: ", BP.predict(test_sample))
print(f"{test_sample} 類別概率分別是: ", BP.predict_proba(test_sample))

test_sample = np.array([[100, -90.1, -9.1]])
print(f"{test_sample} 類別是: ", BP.predict(test_sample))
print(f"{test_sample} 類別概率分別是: ", BP.predict_proba(test_sample))

2 基于BP神經(jīng)網(wǎng)絡(luò)的乳腺癌分類預(yù)測

#%%基于BP神經(jīng)網(wǎng)絡(luò)的乳腺癌分類
#基本庫導(dǎo)入
# 導(dǎo)入乳腺癌數(shù)據(jù)集
from sklearn.datasets import load_breast_cancer
# 導(dǎo)入BP模型
from sklearn.neural_network import MLPClassifier
# 導(dǎo)入訓(xùn)練集分割方法
from sklearn.model_selection import train_test_split 
# 導(dǎo)入預(yù)測指標計算函數(shù)和混淆矩陣計算函數(shù)
from sklearn.metrics import classification_report, confusion_matrix
# 導(dǎo)入繪圖包
import seaborn as sns
import matplotlib.pyplot as plt
# 導(dǎo)入三維顯示工具
from mpl_toolkits.mplot3d import Axes3D
# 導(dǎo)入乳腺癌數(shù)據(jù)集
cancer = load_breast_cancer()
# 查看數(shù)據(jù)集信息
print('breast_cancer數(shù)據(jù)集的長度為:',len(cancer))
print('breast_cancer數(shù)據(jù)集的類型為:',type(cancer))
# 分割數(shù)據(jù)為訓(xùn)練集和測試集
cancer_data = cancer['data']
print('cancer_data數(shù)據(jù)維度為:',cancer_data.shape)
cancer_target = cancer['target']
print('cancer_target標簽維度為:',cancer_target.shape)
cancer_names = cancer['feature_names']
cancer_desc = cancer['DESCR']
#分為訓(xùn)練集與測試集
cancer_data_train,cancer_data_test = train_test_split(cancer_data,test_size=0.2,random_state=42)#訓(xùn)練集
cancer_target_train,cancer_target_test = train_test_split(cancer_target,test_size=0.2,random_state=42)#測試集

#%%# 建立 BP 模型, 采用Adam優(yōu)化器,relu非線性映射函數(shù)
BP = MLPClassifier(solver='adam',activation = 'relu',max_iter = 1000,alpha = 1e-3,hidden_layer_sizes = (64,32, 32),random_state = 1)
# 進行模型訓(xùn)練
BP.fit(cancer_data_train, cancer_target_train)
#%% 進行模型預(yù)測
predict_train_labels = BP.predict(cancer_data_train)
# 可視化真實數(shù)據(jù)
fig = plt.figure()
ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=20, azim=20) 
ax.scatter(cancer_data_train[:, 0], cancer_data_train[:, 1], cancer_data_train[:, 2], marker='o', c=cancer_target_train)
plt.title('True Label Map')
plt.show()
# 可視化預(yù)測數(shù)據(jù)
fig = plt.figure()
ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=20, azim=20) 
ax.scatter(cancer_data_train[:, 0], cancer_data_train[:, 1], cancer_data_train[:, 2], marker='o', c=predict_train_labels)
plt.title('Cancer with BP Model')
plt.show()

#%% 顯示預(yù)測分數(shù)
print("預(yù)測準確率: {:.4f}".format(BP.score(cancer_data_test, cancer_target_test)))
# 進行測試集數(shù)據(jù)的類別預(yù)測
predict_test_labels = BP.predict(cancer_data_test)
print("測試集的真實標簽:\n", cancer_target_test)
print("測試集的預(yù)測標簽:\n", predict_test_labels)
#%% 進行預(yù)測結(jié)果指標統(tǒng)計 統(tǒng)計每一類別的預(yù)測準確率、召回率、F1分數(shù)
print(classification_report(cancer_target_test, predict_test_labels))

#%% 計算混淆矩陣
confusion_mat = confusion_matrix(cancer_target_test, predict_test_labels)
# 打印混淆矩陣
print(confusion_mat)
# 將混淆矩陣以熱力圖的方式顯示
sns.set()
figure, ax = plt.subplots()
# 畫熱力圖
sns.heatmap(confusion_mat, cmap="YlGnBu_r", annot=True, ax=ax)  
# 標題 
ax.set_title('confusion matrix')
# x軸為預(yù)測類別
ax.set_xlabel('predict')  
# y軸實際類別
ax.set_ylabel('true')  
plt.show()

注:之前還做過基于BP神經(jīng)網(wǎng)絡(luò)的人口普查數(shù)據(jù)預(yù)測,有需要的猿友私信

三、Keys

BP神經(jīng)網(wǎng)絡(luò)的要點在于前向傳播和誤差反向傳播,來對參數(shù)進行更新,使得損失最小化。

它是一個迭代算法,基本思想是:

  • 先計算每一層的狀態(tài)和激活值,直到最后一層(即信號是前向傳播的);
  • 計算每一層的誤差,誤差的計算過程是從最后一層向前推進的(反向傳播);
  • 更新參數(shù)(目標是誤差變小)。迭代前面兩個步驟,直到滿足停止準則(比如相鄰兩次迭代的誤差的差別很小)。

886~~~

到此這篇關(guān)于Python機器學(xué)習(xí)應(yīng)用之基于BP神經(jīng)網(wǎng)絡(luò)的預(yù)測篇詳解的文章就介紹到這了,更多相關(guān)Python BP神經(jīng)網(wǎng)絡(luò)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決pytorch 數(shù)據(jù)類型報錯的問題

    解決pytorch 數(shù)據(jù)類型報錯的問題

    這篇文章主要介紹了解決pytorch 數(shù)據(jù)類型報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python?pip超詳細教程之pip的安裝與使用

    Python?pip超詳細教程之pip的安裝與使用

    python中有許多有用的庫,要使用這些第三方庫,需要安裝pip。這篇文章主要為大家介紹了pip的安裝與使用,解決pip下載速度慢的問題,需要的可以參考一下
    2022-07-07
  • Python OpenCV直方圖均衡化詳解

    Python OpenCV直方圖均衡化詳解

    本文中將介紹如何使用OpenCV函數(shù)執(zhí)行直方圖均衡,并將其應(yīng)用于灰度和彩色圖像,以及將亮度歸一化并提高圖像的對比度。感興趣的小伙伴可以了解一下
    2022-02-02
  • Python之np.where()如何替換缺失值

    Python之np.where()如何替換缺失值

    這篇文章主要介紹了Python中的np.where()如何替換缺失值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Pywinauto基礎(chǔ)教程之控件操作

    Pywinauto基礎(chǔ)教程之控件操作

    這篇文章主要給大家介紹了關(guān)于Pywinauto基礎(chǔ)教程之控件操作的相關(guān)資料,pywinauto庫是一個用于在Windows上自動化操作的庫,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-08-08
  • Python實現(xiàn)Ollama的提示詞生成與優(yōu)化

    Python實現(xiàn)Ollama的提示詞生成與優(yōu)化

    這篇文章主要為大家詳細介紹了Python實現(xiàn)Ollama的提示詞生成與優(yōu)化的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • python新手學(xué)習(xí)使用庫

    python新手學(xué)習(xí)使用庫

    在本篇文章里小編給大家整理的一篇關(guān)于python新手學(xué)習(xí)使用庫的相關(guān)方法和知識點,需要的朋友們參考下。
    2020-06-06
  • Python如何繪制概率分布直方圖淺析

    Python如何繪制概率分布直方圖淺析

    項目中在前期經(jīng)常要看下數(shù)據(jù)的分布情況,這對于探究數(shù)據(jù)規(guī)律非常有用,概率分布表示樣本數(shù)據(jù)的模樣,使用Python繪制頻率分布直方圖非常簡潔,因為用的頻次非常高,這篇文章主要給大家介紹了關(guān)于Python如何繪制概率分布直方圖的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • OpenCV物體跟蹤樹莓派視覺小車實現(xiàn)過程學(xué)習(xí)

    OpenCV物體跟蹤樹莓派視覺小車實現(xiàn)過程學(xué)習(xí)

    這篇文章主要介紹了OpenCV物體跟蹤樹莓派視覺小車的實現(xiàn)過程學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10
  • python?使用OpenCV進行曝光融合

    python?使用OpenCV進行曝光融合

    這篇文章主要介紹了python?使用OpenCV進行曝光融合,使用OpenCV的Exposure?Fusion,曝光融合是一種將使用不同曝光設(shè)置拍攝的圖像合成為一張看起來像色調(diào)映射的高動態(tài)范圍(HDR)圖像的圖像的方,下文更多詳細內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-04-04

最新評論

五河县| 安庆市| 土默特左旗| 桓台县| 濮阳市| 噶尔县| 常州市| 鹰潭市| 山东省| 枞阳县| 军事| 昭通市| 壤塘县| 偃师市| 柳林县| 吴旗县| 从化市| 平罗县| 新田县| 曲周县| 鄢陵县| 海阳市| 利川市| 竹北市| 新乡县| 江孜县| 印江| 清丰县| 横山县| 南投县| 承德县| 抚顺县| 招远市| 丰城市| 莲花县| 青神县| 邵东县| 瑞安市| 岢岚县| 教育| 蒙自县|