-
大小: 1.68MB文件類型: .zip金幣: 2下載: 0 次發(fā)布日期: 2023-10-10
- 語言: 其他
- 標簽: 機器學習??數(shù)據(jù)集??
資源簡介
機器學習實戰(zhàn)數(shù)據(jù)集

代碼片段和文件信息
#-*-?coding:utf-8?_*-
“““
@author:KING
@file:?adaboost.py
@time:?2018/08/15
“““
import?numpy?as?np
def?loadSimpData():
????datMat?=?np.matrix([[1.2.1]
?????????????????????[2.1.1]
?????????????????????[1.31.]
?????????????????????[1.1.]
?????????????????????[2.1.]])
????classLabels?=?[1.01.0-1.0-1.01.0]
????return?datMatclassLabels
def?stumpClassify(dataMatrixdimenthreshValthreshIneq):
????retArray?=?np.ones((np.shape(dataMatrix)[0]1))
????if?threshIneq?==?‘lt‘:
????????retArray[dataMatrix[:dimen]?<=?threshVal]?=?-1.0
????else:
????????retArray[dataMatrix[:?dimen]?>?threshVal]?=?-1.0
????return?retArray
def?buildStump(dataArrclassLabelsD):
????dataMatrix?=?np.mat(dataArr)
????labelMat?=?np.mat(classLabels).T
????mn?=?np.shape(dataMatrix)
????numSteps?=?10.0
????bestStump?=?{}
????bestClasEst?=?np.mat(np.zeros((m1)))
????minError?=?np.inf
????for?i?in?range(n):#遍歷特征
????????rangeMin?=?dataMatrix[:i].min()
????????rangeMax?=?dataMatrix[:i].max()
????????stepSize?=?(rangeMax-rangeMin)/numSteps#閾值增長步長
????????for?j?in?range(-1int(numSteps)+1):
????????????for?inequal?in?[‘lt‘‘gt‘]:
????????????????threshVal?=?(rangeMin+float(j)*stepSize)
????????????????predictedVals?=?stumpClassify(dataMatrixithreshValinequal)
????????????????errArr?=?np.mat(np.ones((m1)))
????????????????errArr[predictedVals==labelMat]=0
????????????????weightedError?=?D.T*errArr
????????????????print(“split:dim?%dthresh?%.2fthresh?inequal:%sthe?weightedError?is?:%.3f“?%(
????????????????????ithreshValinequalweightedError))
????????????????if?weightedError ????????????????????minError?=?weightedError
????????????????????bestClasEst?=?predictedVals.copy()
????????????????????bestStump[‘dim‘]?=?i
????????????????????bestStump[‘thresh‘]=threshVal
????????????????????bestStump[‘ineq‘]?=?inequal
????return?bestStumpminErrorbestClasEst
def?adaBoostTrainDS(dataArrclassLabelsnumIt?=?40):
????weakClassArr?=?[]
????m?=?np.shape(dataArr)[0]
????D?=?np.mat(np.ones((m1))/m)
????aggClassEst?=?np.mat(np.zeros((m1)))
????for?i?in?range(numIt):
????????bestStumperrorclassEst?=?buildStump(dataArrclassLabelsD)#第i個弱分類器
????????print(“D:“D.T)
????????alpha?=?float(0.5*np.log((1.0-error)/np.fmax(error1e-16)))
????????bestStump[‘a(chǎn)lpha‘]?=?alpha
????????weakClassArr.append(bestStump)
????????print(“classEst:?“classEst.T)#該分類器的分類結果
????????expon?=?np.multiply(-1*alpha*np.mat(classLabels).TclassEst)
????????D?=?np.multiply(Dnp.exp(expon))
????????D?=?D/D.sum()
????????aggClassEst+=alpha*classEst#與此前的分類器同時工作給出分類結果
????????print(“aggClassEst:?“aggClassEst.T)
????????aggErrors?=?np.multiply(np.sign(aggClassEst)!=?np.mat(classLabels).Tnp.ones((m1)))
????????errorRate?=?aggErrors.sum()/m
????????print(“total?error:“errorRate“\n“)
????????if?errorRate?==?0.0:
????????????break
????return?weakClassArr
def?adaClassify(dataToClassclassifierArr):
????dataMatrix?=?np.mat(dataToClass)
????m?=?np.shape(dataMatrix)[0]
????aggClassEst?=?np.mat(np.
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\.idea\
?????文件?????????455??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\.idea\AdaBoost.iml
?????文件?????????185??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\.idea\misc.xm
?????文件?????????268??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\.idea\modules.xm
?????文件????????6905??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\.idea\workspace.xm
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\.ipynb_checkpoints\
?????文件????????6963??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\.ipynb_checkpoints\AdaBoost-checkpoint.ipynb
?????文件????????8412??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\AdaBoost.ipynb
?????文件????????3456??2018-08-15?07:18??Machine-Learning-in-Action-master\AdaBoost\adaboost.py
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\.idea\
?????文件?????????398??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\.idea\Logistic回歸.iml
?????文件?????????185??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\.idea\misc.xm
?????文件?????????280??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\.idea\modules.xm
?????文件????????9563??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\.idea\workspace.xm
?????文件????????3722??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\horseColicTest.txt
?????文件???????60357??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\horseColicTraining.txt
?????文件????????4007??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\logRegres.py
?????文件????????4507??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\logRegres.pyc
?????文件????????2087??2018-08-15?07:18??Machine-Learning-in-Action-master\Logistic回歸\testSet.txt
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\MLiA_SourceCode\
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\MLiA_SourceCode\machinelearninginaction\
?????文件??????????31??2018-08-15?07:18??Machine-Learning-in-Action-master\README.txt
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\SVM\
?????目錄???????????0??2018-08-15?07:18??Machine-Learning-in-Action-master\SVM\.idea\
?????文件?????????398??2018-08-15?07:18??Machine-Learning-in-Action-master\SVM\.idea\SVM.iml
?????文件?????????185??2018-08-15?07:18??Machine-Learning-in-Action-master\SVM\.idea\misc.xm
?????文件?????????258??2018-08-15?07:18??Machine-Learning-in-Action-master\SVM\.idea\modules.xm
?????文件???????16956??2018-08-15?07:18??Machine-Learning-in-Action-master\SVM\.idea\workspace.xm
............此處省略3581個文件信息
評論
共有 條評論