赞
踩
importnumpyasnpfromsklearn.treeimportDecisionTreeClassifierclassFixedFeatureRFC:def__init__(self,n_estimators=10,random_state=None):self.n_estimators=n_estimatorsifrandom_stateisNone:self.random_state=np.random.RandomState()deffit(self,X,y,feats_fixed=None,max_features=None,bootstrap_frac=0.8):"""
feats_fixed: indices of features (columns of X) to be
always used to train each estimator
max_features: number of features that each estimator will use,
including the fixed features.
bootstrap_frac: size of bootstrap sample that each estimator will use.
"""self.estimators=[]self.feats_used=[]self.n_classes=np.unique(y).shape[0]iffeats_fixedisNone:feats_fixed=[]ifmax_featuresisNone:max_features=X.shape[1]n_samples=X.shape[0]n_bs=int(bootstrap_frac*n_samples)feats_fixed=list(feats_fixed)feats_all=range(X.shape[1])random_choice_size=max_features-len(feats_fixed)feats_choosable=set(feats_all).difference(set(feats_fixed))feats_choosable=np.array(list(feats_choosable))foriinrange(self.n_estimators):chosen=self.random_state.choice(feats_choosable,size=random_choice_size,replace=False)feats=feats_fixed+list(chosen)self.feats_used.append(feats)bs_sample=self.random_state.choice(n_samples,size=n_bs,replace=True)dtc=DecisionTreeClassifier(random_state=self.random_state)dtc.fit(X[bs_sample][:,feats],y[bs_sample])self.estimators.append(dtc)defpredict_proba(self,X):out=np.zeros((X.shape[0],self.n_classes))foriinrange(self.n_estimators):out+=self.estimators[i].predict_proba(X[:,self.feats_used[i]])returnout/self.n_estimatorsdefpredict(self,X):returnself.predict_proba(X).argmax(axis=1)defscore(self,X,y):return(self.predict(X)==y).mean()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。