classEnsembleClasifier():def__init__(self,base_classifier,labels):self.classifier=[base_classifier]self.labels=labelsdefadd_classifier(self,classifier):self.classifier.append(classifier)defremove_last_classifier(self):self.classifier.pop(-1)defpredict_proba(self,X):returnnp.array([clf.predict_proba(X)forclfinself.classifier]).sum(axis=0)/len(self.classifier)defpredict(self,X):returnlabels[np.argmax(self.predict_proba(X),axis=1)]deferror(self,X,y):return1-accuracy_score(y,ensembleClasifier.predict(X))classArtificial_data():def__init__(self,X,y,dtypes):self.dtypes={}self._generator={}self.labels=y.unique()forc,dtypeinzip(X.columns,dtypes):self.dtypes[c]=dtypeifdtype=='numeric':self._generator[c]={'mean':X[c].mean(),'std':X[c].std()}else:unique_values=X[c].value_counts()/X.shape[0]self._generator[c]={'values':unique_values.index,'prob':unique_values.values}defsample_generator(self,ensembleClasifier,nb_samples=1):syn_X=pd.DataFrame()forcinself.dtypes.keys():ifself.dtypes[c]=='numeric':syn_X[c]=np.random.normal(self._generator[c]['mean'],self._generator[c]['std'],nb_samples)else:syn_X[c]=np.random.choice(self._generator[c]['values'],p=self._generator[c]['prob'],size=nb_samples,replace=True)p_hat=ensembleClasifier.predict_proba(syn_X)p_hat[p_hat==0]=1e-5inverse_p=1/p_hat
new_p=inverse_p/inverse_p.sum(axis=1)[:,np.newaxis]syn_y=[np.random.choice(self.labels,p=new_p[i])foriinrange(nb_samples)]returnsyn_X,syn_yimportnumpyasnpimportpandasaspdfromsklearn.model_selectionimporttrain_test_splitfromsklearn.treeimportDecisionTreeClassifierfromsklearn.metricsimportaccuracy_scorefromsklearnimportdatasets
iris=datasets.load_iris()X,y=iris.data,iris.target
X_train_base,X_test,y_train_base,y_test=train_test_split(pd.DataFrame(X),pd.Series(y),test_size=0.3,random_state=100)# dtypes=['numeric' for _ in range(7)] + ['nominal'] #use this for abalone datasetdtypes=['numeric'for_inrange(4)]np.random.seed(1)artifical_data=Artificial_data(X_train_base,y_train_base,dtypes)c_size=15i_max=300R_size=len(X_train_base)i=1trails=1labels=np.unique(y_train_base)clf_entropy=DecisionTreeClassifier(random_state=1,max_depth=2)clf_entropy.fit(X_train_base,y_train_base)ensembleClasifier=EnsembleClasifier(clf_entropy,labels)error_bst=ensembleClasifier.error(X_train_base,y_train_base)while(i