我正在尝试使用堆叠将两种机器学习算法结合起来以获得更好的结果,但在某些方面失败了。这是我的代码:
class Ensemble(threading.Thread):“与三个分类模型堆叠以提高预测的准确性” def init (self, X, Y, XT, YT, accLabel=None): threading.Thread。init (self) self.X = X self.Y = Y self.XT=XT self.YT=YT self.accLabel= accLabel
def Stacking(self,model,n_fold,train,test,y):
folds=StratifiedKFold(n_splits=n_fold,random_state=1)
test_pred=np.empty((test.shape[0],1),float)
train_pred=np.empty((0,1),float)
for train_indices,val_indices in folds.split(train,y):
x_train,x_val=train.iloc[train_indices],train.iloc[val_indices]
y_train,y_val=y.iloc[train_indices],y.iloc[val_indices]
model.fit(X=x_train,y=y_train)
train_pred=np.append(train_pred,model.predict(x_val))
test_pred=np.append(test_pred,model.predict(test))
return test_pred.reshape(-1,1),train_pred
def run(self):
X = np.zeros(self.X.shape)
Y = np.zeros(self.Y.shape)
XT = np.zeros(self.XT.shape)
YT = np.zeros(self.YT.shape)
np.copyto(X, self.X)
np.copyto(Y, self.Y)
np.copyto(XT, self.XT)
np.copyto(YT, self.YT)
model1 = tree.DecisionTreeClassifier(random_state=1)
n_fold=4
test_pred1 ,train_pred1=self.Stacking(model1, n_fold, X, XT, Y)
train_pred1=pd.DataFrame(train_pred1)
test_pred1=pd.DataFrame(test_pred1)
model2 = KNeighborsClassifier()
test_pred2 ,train_pred2=self.Stacking(model2, n_fold, …Run Code Online (Sandbox Code Playgroud)