随机森林是一种有效的分类预测方法,它有很高 的分类精度,对于噪声和异常值有较好的稳健性,且 具有较强的泛化能力。Breiman在论文中提出了随机 森林的数学理论,证明随机森林不会出现决策树的过 拟合问题。
- 是一种不等性度量;
- 通常用来度量收入不平衡,可以用来度量任何不均匀分布;
- 是介于0~1之间的数,0-完全相等,1-完全不相等;
- 总体内包含的类别越杂乱,GINI指数就越大(跟熵的概念很相似)
def createTree(sampset,defaulVal):
#print "create"
if not sampset:
return defaulVal#,"no sample"
labelList=[s[-1] for s in sampset]
if labelList.count(labelList[0])==len(labelList):
return int(labelList[0])#,"same class"
newDefault=majorClass(labelList)
bestFeat,bestFeatVal=findBestFea(sampset)
#print bestFeat,bestFeatVal
#当前树节点
node=(bestFeat,bestFeatVal,len(labelList))
myTree={node:{}}
#左子树和右子树
lset=[s for s in sampset if s[bestFeat]<=bestFeatVal]
rset=[s for s in sampset if s[bestFeat]>bestFeatVal]
myTree[node]['left']=createTree(lset,newDefault)
myTree[node]['right']=createTree(rset,newDefault)
def findBestFea(sampset):
features=range(617)
feaList=random.sample(features,15)
bestFea=0
bestpar=0.0
mingini=1.0
for fea in feaList:
#print fea
tup=[(s[fea],s[-1]) for s in sampset]
tup.sort()
par=[-0.9,-0.5,0,0.5,0.9]
bar=0
for t in tup:
if t[0]>parmem:
break
bar+=1
nllab=[mem[1] for mem in tup[0:bar]]
llab=[mem[1] for mem in tup[bar:]]
nll={}
for l in nllab:
if l not in nll.keys():
nll[l]=0
nll[l]+=1
gini1=1.0
#print nll
for k in nll.keys():
gini1-=pow(float(nll[k])/len(nllab),2)
ll={}
for l in llab:
if l not in ll.keys():
ll[l]=0
ll[l]+=1
gini2=1.0
for k in ll.keys():
gini2-=pow(float(ll[k])/len(llab),2)
gini=float(len(nllab))/len(tup)*gini1+float(len(llab))/len(tup)*gini2
#print gini
if gini < mingini:
mingini=gini
bestpar=parmem
bestFea=fea
#pdb.set_trace()
def classify(inputTree,testList):
firstnode=inputTree.keys()[0]
firstFea=inputTree.keys()[0][0]
firstPar=inputTree.keys()[0][1]
testFea=testList[firstFea]
secondDict=inputTree[firstnode]
if testFea<=firstPar:
if type(secondDict['left']).__name__=='dict':
classLabel=classify(secondDict['left'],testList)
else:
classLabel=secondDict['left']
else:
if type(secondDict['right']).__name__=='dict':
classLabel=classify(secondDict['right'],testList)
else:
classLabel=secondDict['right']
#有放回抽样
def repeatRandomSamp(dataSet):
samples=[]
a=len(dataSet)
for i in xrange(6238):
samples.append(dataSet[random.randint(0,len(dataSet)-1)])
return samples
随机抽取特征子集实现
for x in xrange(4):
process=multiprocessing.Process(target=partcre,args=(ss[x],trees[x]))
process.start()
pcs.append(process)
for x in xrange(4):
result.append(pool.apply_async(partcre,(ss[x],)))
树的数量 |
随机选取的训练样本数 |
随机选取的特征数 |
特征值的切分点个数 |
是否并行化 |
时间 |
分类精度(kaggle测试集) |
40 |
6238 |
25 |
5 |
NO |
499 |
0.93162 |
40 |
6238 |
25 |
5 |
Multiprocessing |
253 |
0.9328 |
40 |
6238 |
25 |
5 |
pool |
292 |
0.93162 |
树的数量 |
随机选取的训练样本数 |
随机选取的特征数 |
特征值的切分点个数 |
是否并行化 |
时间 |
分类精度(kaggle测试集) |
20 |
6238 |
25 |
5 |
Multiprocessing |
133 |
0.92735 |
40 |
6238 |
25 |
5 |
Multiprocessing |
238 |
0.93162 |
80 |
6238 |
25 |
5 |
Multiprocessing |
536 |
0.93504 |
120 |
6238 |
25 |
5 |
Multiprocessing |
766 |
0.94017 |
160 |
6238 |
25 |
5 |
Multiprocessing |
1040 |
0.94188 |
200 |
6238 |
25 |
5 |
Multiprocessing |
1246 |
0.94017 |
400 |
6238 |
25 |
5 |
Multiprocessing |
2757 |
0.94274 |
500 |
6238 |
25 |
5 |
Multiprocessing |
3782 |
0.93932 |
树的数量 |
随机选取的训练样本数 |
随机选取的特征数 |
特征值的切分点个数 |
是否并行化 |
时间 |
分类精度(kaggle测试集) |
40 |
1000 |
25 |
5 |
Multiprocessing |
37 |
0.91624 |
40 |
2000 |
25 |
5 |
Multiprocessing |
79 |
0.91880 |
40 |
3000 |
25 |
5 |
Multiprocessing |
114 |
0.92821 |
40 |
4000 |
25 |
5 |
Multiprocessing |
174 |
0.92735 |
40 |
5000 |
25 |
5 |
Multiprocessing |
196 |
0.92821 |
40 |
6238 |
25 |
5 |
Multiprocessing |
253 |
0.9328 |
树的数量 |
随机选取的训练样本数 |
随机选取的特征数 |
特征值的切分点个数 |
是否并行化 |
时间 |
分类精度(kaggle测试集) |
40 |
6238 |
5 |
5 |
Multiprocessing |
61 |
0.90085 |
40 |
6238 |
10 |
5 |
Multiprocessing |
129 |
0.92479 |
40 |
6238 |
15 |
5 |
Multiprocessing |
165 |
0.93932 |
40 |
6238 |
20 |
5 |
Multiprocessing |
198 |
0.92650 |
40 |
6238 |
25 |
5 |
Multiprocessing |
253 |
0.9328 |
40 |
6238 |
50 |
5 |
Multiprocessing |
501 |
0.93333 |
40 |
6238 |
100 |
5 |
Multiprocessing |
963 |
0.92393 |
树的数量 |
随机选取的训练样本数 |
随机选取的特征数 |
特征值的切分点个数 |
是否并行化 |
时间 |
分类精度(kaggle测试集) |
40 |
6238 |
15 |
1 |
Multiprocessing |
92 |
0.88120 |
40 |
6238 |
15 |
3 |
Multiprocessing |
154 |
0.93248 |
40 |
6238 |
15 |
5 |
Multiprocessing |
165 |
0.93932 |
40 |
6238 |
15 |
11 |
Multiprocessing |
304 |
0.93504 |
40 |
6238 |
15 |
19 |
Multiprocessing |
480 |
0.93419 |