当前位置:   article > 正文

python randomforest 离散值_使RandomForestClassifier在训练期间确定选择一个变量

python 随机森林设置离散特征

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()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/604255
推荐阅读
相关标签
  

闽ICP备14008679号