def predict(self, X): """Hard decision.""" # print("PREDICT") # Check is fit had been called check_is_fitted(self, "classes_") # Input validation X = check_array(X) if X.shape[1] != self.X_.shape[1]: raise ValueError("number of features does not match") X_dsel = self.previous_X y_dsel = self.previous_y if self.oversampled: ros = RandomOverSampler(random_state=42) X_dsel, y_dsel = ros.fit_resample(X_dsel, y_dsel) if self.desMethod == "KNORAE": des = KNORAE(self.ensemble_, random_state=42) elif self.desMethod == "KNORAU": des = KNORAU(self.ensemble_, random_state=42) elif self.desMethod == "LCA": des = LCA(self.ensemble_, random_state=42) elif self.desMethod == "Rank": des = Rank(self.ensemble_, random_state=42) else: des = KNORAE(self.ensemble_, random_state=42) des.fit(X_dsel, y_dsel) prediction = des.predict(X) return prediction
def predict(self, X): # Check is fit had been called check_is_fitted(self, "classes_") # Input validation X = check_array(X) if X.shape[1] != self.X_.shape[1]: raise ValueError("number of features does not match") X_dsel = self.previous_X y_dsel = self.previous_y unique, counts = np.unique(y_dsel, return_counts=True) k_neighbors = 5 if counts[0] - 1 < 5: k_neighbors = counts[0] - 1 if self.oversampler == "SMOTE" and k_neighbors > 0: smote = SMOTE(random_state=42, k_neighbors=k_neighbors) X_dsel, y_dsel = smote.fit_resample(X_dsel, y_dsel) elif self.oversampler == "svmSMOTE" and k_neighbors > 0: try: svmSmote = SVMSMOTE(random_state=42, k_neighbors=k_neighbors) X_dsel, y_dsel = svmSmote.fit_resample(X_dsel, y_dsel) except ValueError: pass elif self.oversampler == "borderline1" and k_neighbors > 0: borderlineSmote1 = BorderlineSMOTE(random_state=42, k_neighbors=k_neighbors, kind='borderline-1') X_dsel, y_dsel = borderlineSmote1.fit_resample(X_dsel, y_dsel) elif self.oversampler == "borderline2" and k_neighbors > 0: borderlineSmote2 = BorderlineSMOTE(random_state=42, k_neighbors=k_neighbors, kind='borderline-2') X_dsel, y_dsel = borderlineSmote2.fit_resample(X_dsel, y_dsel) elif self.oversampler == "ADASYN" and k_neighbors > 0: try: adasyn = ADASYN(random_state=42, n_neighbors=k_neighbors) X_dsel, y_dsel = adasyn.fit_resample(X_dsel, y_dsel) except RuntimeError: pass except ValueError: pass elif self.oversampler == "SLS" and k_neighbors > 0: sls = Safe_Level_SMOTE(n_neighbors=k_neighbors) X_dsel, y_dsel = sls.sample(X_dsel, y_dsel) if self.desMethod == "KNORAE": des = KNORAE(self.ensemble_, random_state=42) elif self.desMethod == "KNORAU": des = KNORAU(self.ensemble_, random_state=42) elif self.desMethod == "KNN": des = DESKNN(self.ensemble_, random_state=42) elif self.desMethod == "Clustering": des = DESClustering(self.ensemble_, random_state=42) else: des = KNORAE(self.ensemble_, random_state=42) if len(self.ensemble_) < 2: prediction = self.ensemble_[0].predict(X) else: des.fit(X_dsel, y_dsel) prediction = des.predict(X) return prediction