def test_CNN(x, y): print('Condensed Nearest Neighbour') CNN = CondensedNearestNeighbour(verbose=verbose) cnnx, cnny = CNN.fit_transform(x, y) print('One-Sided Selection') OSS = OneSidedSelection(verbose=verbose) ossx, ossy = OSS.fit_transform(x, y) print('BalanceCascade') BS = BalanceCascade(verbose=verbose) bsx, bsy = BS.fit_transform(x, y)
def apply_sampling(X_data, Y_data, sampling, n_states, maxlen): ratio = float(np.count_nonzero(Y_data == 1)) / \ float(np.count_nonzero(Y_data == 0)) X_data = np.reshape(X_data, (len(X_data), n_states * maxlen)) # 'Random over-sampling' if sampling == 'OverSampler': OS = OverSampler(ratio=ratio, verbose=True) # 'Random under-sampling' elif sampling == 'UnderSampler': OS = UnderSampler(verbose=True) # 'Tomek under-sampling' elif sampling == 'TomekLinks': OS = TomekLinks(verbose=True) # Oversampling elif sampling == 'SMOTE': OS = SMOTE(ratio=1, verbose=True, kind='regular') # Oversampling - Undersampling elif sampling == 'SMOTETomek': OS = SMOTETomek(ratio=ratio, verbose=True) # Undersampling elif sampling == 'OneSidedSelection': OS = OneSidedSelection(verbose=True) # Undersampling elif sampling == 'CondensedNearestNeighbour': OS = CondensedNearestNeighbour(verbose=True) # Undersampling elif sampling == 'NearMiss': OS = NearMiss(version=1, verbose=True) # Undersampling elif sampling == 'NeighbourhoodCleaningRule': OS = NeighbourhoodCleaningRule(verbose=True) # ERROR: WRONG SAMPLER, TERMINATE else: print('Wrong sampling variable you have set... Exiting...') sys.exit() # print('shape ' + str(X.shape)) X_data, Y_data = OS.fit_transform(X_data, Y_data) return X_data, Y_data
def one_sided_selection(self): OSS = OneSidedSelection(size_ngh=51, n_seeds_S=51, verbose=self.verbose) ossx, ossy = OSS.fit_transform(self.x, self.y) print "OSS Transforemd" return ossx, ossy
colnames = ['old_index','job_id', 'task_idx','sched_cls', 'priority', 'cpu_requested', 'mem_requested', 'disk', 'violation'] tain_path = r'/home/askrey/Dropbox/Project_step_by_step/3_create_database/csvs/frull_db_2.csv' X = pd.read_csv(tain_path, header = None, index_col = False ,names = colnames, skiprows = [0], usecols = [3,4,5,6,7]) y = pd.read_csv(tain_path, header = None, index_col = False ,names = colnames, skiprows = [0], usecols = [8]) y = y['violation'].values # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.333, random_state=0) main_x = X.values main_y = y verbose = False # 'One-Sided Selection' OSS = OneSidedSelection(size_ngh=51, n_seeds_S=51, verbose=verbose) x, y = OSS.fit_transform(main_x, main_y) ratio = float(np.count_nonzero(y==1)) / float(np.count_nonzero(y==0)) X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=.333, random_state=0) from sklearn.ensemble import RandomForestClassifier from sklearn.cross_validation import cross_val_score clf = RandomForestClassifier(n_estimators=10) scores = cross_val_score(clf, X_test, y_test) y_pred = clf.fit(X_train, y_train).predict(X_test) y_score = clf.fit(X_train, y_train).predict_proba(X_test)[:,1]
CC = ClusterCentroids(verbose=verbose) ccx, ccy = CC.fit_transform(x, y) # 'NearMiss-1' NM1 = NearMiss(version=1, verbose=verbose) nm1x, nm1y = NM1.fit_transform(x, y) # 'NearMiss-2' NM2 = NearMiss(version=2, verbose=verbose) nm2x, nm2y = NM2.fit_transform(x, y) # 'NearMiss-3' NM3 = NearMiss(version=3, verbose=verbose) nm3x, nm3y = NM3.fit_transform(x, y) # 'Condensed Nearest Neighbour' CNN = CondensedNearestNeighbour(size_ngh=51, n_seeds_S=51, verbose=verbose) cnnx, cnny = CNN.fit_transform(x, y) # 'One-Sided Selection' OSS = OneSidedSelection(size_ngh=51, n_seeds_S=51, verbose=verbose) ossx, ossy = OSS.fit_transform(x, y) # 'Neighboorhood Cleaning Rule' NCR = NeighbourhoodCleaningRule(size_ngh=51, verbose=verbose) ncrx, ncry = NCR.fit_transform(x, y) # Apply PCA to be able to visualise the results usx_vis = pca.transform(usx) tlx_vis = pca.transform(tlx) ccx_vis = pca.transform(ccx) nm1x_vis = pca.transform(nm1x) nm2x_vis = pca.transform(nm2x) nm3x_vis = pca.transform(nm3x) cnnx_vis = pca.transform(cnnx) ossx_vis = pca.transform(ossx) ncrx_vis = pca.transform(ncrx)