def nearmiss1(X, y): undersample = NearMiss(version=1, n_neighbors=3) X, y = undersample.fit_resample(X, y) counter = Counter(y) print("NearMiss-1", counter) plot_dataset(X, y, counter)
def weighted_neural_network(n_input): model = define_model(n_input) weights = {0: 1, 1: 100} history = model.fit(trainX, trainy, class_weight=weights, epochs=100, verbose=0) yhat = model.predict(testX) score = roc_auc_score(testy, yhat) print('{0:<30} ROC AUC: {1:.3f}'.format('Weighted Neural Network', score)) plot_dataset(testX, array([round(i[0]) for i in yhat]), Counter(array([round(i[0]) for i in yhat])))
# borderline-SMOTE for imbalanced dataset from collections import Counter from sklearn.datasets import make_classification from imblearn.over_sampling import BorderlineSMOTE from plotDataset import plot_dataset X, y = make_classification(n_samples=10000, n_features=2, n_redundant=0, n_clusters_per_class=1, weights=[0.99], flip_y=0, random_state=1) counter = Counter(y) print(counter) plot_dataset(X, y, counter) oversample = BorderlineSMOTE() X, y = oversample.fit_resample(X, y) counter = Counter(y) print(counter) plot_dataset(X, y, counter)
score = roc_auc_score(testy, yhat) print('{0:<30} ROC AUC: {1:.3f}'.format('Standard Neural Network', score)) def weighted_neural_network(n_input): model = define_model(n_input) weights = {0: 1, 1: 100} history = model.fit(trainX, trainy, class_weight=weights, epochs=100, verbose=0) yhat = model.predict(testX) score = roc_auc_score(testy, yhat) print('{0:<30} ROC AUC: {1:.3f}'.format('Weighted Neural Network', score)) plot_dataset(testX, array([round(i[0]) for i in yhat]), Counter(array([round(i[0]) for i in yhat]))) if __name__ == '__main__': trainX, trainy, testX, testy = prepare_data() n_input = trainX.shape[1] plot_dataset(testX, testy, Counter(testy)) standard_neural_network(n_input) weighted_neural_network(n_input)