-
大小:文件類型: .zip金幣: 1下載: 0 次發(fā)布日期: 2023-06-15
- 語言: Python
- 標(biāo)簽: python3??人工智能??機(jī)器學(xué)習(xí)??第二版??
資源簡介
《機(jī)器學(xué)習(xí)實戰(zhàn)》python3完美運行代碼
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
“““
Created?on?Thu?Jul?26?09:22:46?2018
@author:?wzy
“““
import?numpy?as?np
import?matplotlib.pyplot?as?plt
“““
函數(shù)說明:創(chuàng)建單層決策樹的數(shù)據(jù)集
Parameters:
????None
????
Returns:
????dataMat?-?數(shù)據(jù)矩陣
????classLabels?-?數(shù)據(jù)標(biāo)簽
Modify:
????2018-07-26
“““
def?loadsimpData():
????datMat?=?np.matrix([[1.??2.1]
????????????????????????[1.5?1.6]
????????????????????????[1.3?1.?]
????????????????????????[1.??1.?]
????????????????????????[2.??1.?]])
????classLabels?=?[1.0?1.0?-1.0?-1.0?1.0]
????return?datMat?classLabels
“““
函數(shù)說明:單層決策樹分類函數(shù)
Parameters:
????dataMatrix?-?數(shù)據(jù)矩陣
????dimen?-?第dimen列,也就是第幾個特征
????threshVal?-?閾值
????threshIneq?-?標(biāo)志
????
Returns:
????retArray?-?分類結(jié)果
Modify:
????2018-07-26
“““
def?stumpClassify(dataMatrix?dimen?threshVal?threshIneq):
????#?初始化retArray為全1列向量
????retArray?=?np.ones((np.shape(dataMatrix)[0]?1))
????if?threshIneq?==?‘lt‘:
????????#?如果小于閾值則賦值為-1
????????retArray[dataMatrix[:?dimen]?<=?threshVal]?=?-1.0
????else:
????????#?如果大于閾值則賦值為-1
????????retArray[dataMatrix[:?dimen]?>?threshVal]?=?-1.0
????return?retArray
“““
函數(shù)說明:找到數(shù)據(jù)集上最佳的單層決策樹
Parameters:
????dataArr?-?數(shù)據(jù)矩陣
????classLabels?-?數(shù)據(jù)標(biāo)簽
????D?-?樣本權(quán)重每個樣本權(quán)重相等?1/n
????
Returns:
????bestStump?-?最佳單層決策樹信息
????minError?-?最小誤差
????bestClassEst?-?最佳的分類結(jié)果
Modify:
????2018-07-26
“““
def?buildStump(dataArr?classLabels?D):
????#?輸入數(shù)據(jù)轉(zhuǎn)為矩陣(5?2)
????dataMatrix?=?np.mat(dataArr)
????#?將標(biāo)簽矩陣進(jìn)行轉(zhuǎn)置(5?1)
????labelMat?=?np.mat(classLabels).T
????#?m=5?n=2
????m?n?=?np.shape(dataMatrix)
????numSteps?=?10.0
????bestStump?=?{}
????#?(5?1)全零列矩陣
????bestClasEst?=?np.mat(np.zeros((m?1)))
????#?最小誤差初始化為正無窮大inf
????minError?=?float(‘inf‘)
????#?遍歷所有特征
????for?i?in?range(n):
????????#?找到(每列)特征中的最小值和最大值
????????rangeMin?=?dataMatrix[:?i].min()
????????rangeMax?=?dataMatrix[:?i].max()
????????#?計算步長
????????stepSize?=?(rangeMax?-?rangeMin)?/?numSteps
????????for?j?in?range(-1?int(numSteps)?+?1):
????????????#?大于和小于的情況均遍歷,lt:Less?than??gt:greater?than
????????????for?inequal?in?[‘lt‘?‘gt‘]:
????????????????#?計算閾值
????????????????threshVal?=?(rangeMin?+?float(j)?*?stepSize)
????????????????#?計算分類結(jié)果
????????????????predictedVals?=?stumpClassify(dataMatrix?i?threshVal?inequal)
????????????????#?初始化誤差矩陣
????????????????errArr?=?np.mat(np.ones((m?1)))
????????????????#?分類正確的,賦值為0
????????????????errArr[predictedVals?==?labelMat]?=?0
????????????????#?計算誤差
????????????????weightedError?=?D.T?*?errArr
????????????????print(“split:?dim?%d?thresh?%.2f?thresh?ineqal:?%s?the?weighted?error?is?%.3f“?%?(i?threshVal?inequal?weightedError))
????????????????#?找到誤差最小的分類方式
????????????????if?weightedError?????????????????????minError?=?weightedError
????????????????????bestClasEst?=?predictedVals.copy()
????????????????????bestStump[‘dim‘]?=?i
????????????????????bestStump[‘thresh‘]?=?threshVal
????????????????????bestStump[‘ineq‘]?=?inequa
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-12-17?08:25??MachineLearning\
?????目錄???????????0??2018-07-26?09:23??MachineLearning\AdaBoost_Project1\
?????文件????????7450??2018-07-26?20:57??MachineLearning\AdaBoost_Project1\AdaBoost.py
?????目錄???????????0??2018-12-17?08:23??MachineLearning\AdaBoost_Project2\
?????文件????????7109??2018-07-26?21:51??MachineLearning\AdaBoost_Project2\AdaBoost.py
?????文件???????13547??2018-06-24?16:10??MachineLearning\AdaBoost_Project2\horseColicTest2.txt
?????文件???????60479??2018-06-24?16:10??MachineLearning\AdaBoost_Project2\horseColicTraining2.txt
?????目錄???????????0??2018-12-17?08:23??MachineLearning\AdaBoost_Project3\
?????文件????????1729??2018-07-27?15:19??MachineLearning\AdaBoost_Project3\AdaBoost.py
?????文件???????13547??2018-06-24?16:10??MachineLearning\AdaBoost_Project3\horseColicTest2.txt
?????文件???????60479??2018-06-24?16:10??MachineLearning\AdaBoost_Project3\horseColicTraining2.txt
?????目錄???????????0??2018-12-17?08:23??MachineLearning\AdaBoost_Project4\
?????文件????????7990??2018-07-29?15:16??MachineLearning\AdaBoost_Project4\AdaBoost.py
?????文件???????60479??2018-06-24?16:10??MachineLearning\AdaBoost_Project4\horseColicTraining2.txt
?????目錄???????????0??2018-12-17?08:23??MachineLearning\Apriori_Project1\
?????文件????????7857??2018-08-05?17:14??MachineLearning\Apriori_Project1\Apriori.py
?????文件??????570408??2011-07-13?09:49??MachineLearning\Apriori_Project1\mushroom.dat
?????目錄???????????0??2018-07-29?16:45??MachineLearning\BayesianAnalysisWithPython\
?????文件????????1481??2018-07-29?17:16??MachineLearning\BayesianAnalysisWithPython\Gauss.py
?????目錄???????????0??2018-12-17?08:23??MachineLearning\Bayes_Project1\
?????文件????????7548??2018-07-21?15:29??MachineLearning\Bayes_Project1\Bayes.py
?????目錄???????????0??2018-12-17?08:23??MachineLearning\Bayes_Project2\
?????文件????????8969??2018-07-21?16:40??MachineLearning\Bayes_Project2\Bayes.py
?????目錄???????????0??2018-12-17?08:23??MachineLearning\Bayes_Project2\email\
?????目錄???????????0??2018-12-17?08:23??MachineLearning\Bayes_Project2\email\ham\
?????文件?????????141??2018-06-24?16:10??MachineLearning\Bayes_Project2\email\ham\1.txt
?????文件??????????82??2018-06-24?16:10??MachineLearning\Bayes_Project2\email\ham\10.txt
?????文件?????????122??2018-06-24?16:10??MachineLearning\Bayes_Project2\email\ham\11.txt
?????文件?????????172??2018-06-24?16:10??MachineLearning\Bayes_Project2\email\ham\12.txt
?????文件?????????164??2018-06-24?16:10??MachineLearning\Bayes_Project2\email\ham\13.txt
?????文件?????????162??2018-06-24?16:10??MachineLearning\Bayes_Project2\email\ham\14.txt
............此處省略8959個文件信息
評論
共有 條評論