def test_fit_max_chunk(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=20) Nprobe, Nsample = (5, 4) X = np.arange(Nprobe * Nsample).reshape((Nsample, Nprobe)) y = np.array(['A', 'A', 'B', 'B']) with pytest.raises(ValueError): dnet.fit(X, y)
def test_fit_transform(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) Nprobe, Nsample = (5, 4) X = np.arange(Nprobe * Nsample).reshape((Nsample, Nprobe)) y = np.array(['A', 'A', 'B', 'B']) Xnew = dnet.fit_transform(X, y) assert Xnew.shape == (Nsample, 2) assert np.allclose(Xnew, X[:, :2])
def test_set_signature(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) Nprobe, Nsample = (5, 4) X = np.arange(Nprobe * Nsample).reshape((Nsample, Nprobe)) y = np.array(['A', 'A', 'B', 'B']) dnet.fit(X, y) with pytest.raises(ValueError): signature = dnet.set_signature(-1)
def test_score(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) Nprobe, Nsample = (5, 4) X = np.arange(Nprobe * Nsample).reshape((Nsample, Nprobe)) y = np.array(['A', 'A', 'B', 'B']) dnet.fit(X, y) score = dnet.score(X, y) assert score == 0
def test_predict(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) Nprobe, Nsample = (5, 4) X = np.full(shape=(Nsample, Nprobe), fill_value=42.) # y = np.zeros(shape=(Nsample), dtype=int) y = np.array(['A', 'A', 'B', 'B']) with pytest.warns(RuntimeWarning): dnet.fit(X, y) y_pred = dnet.predict(X) assert all(x == 0 for x in y_pred)
def test_random_fit(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) Nprobe, Nsample = (5, 4) X = np.arange(Nprobe * Nsample).reshape((Nsample, Nprobe)) y = np.array(['A', 'A', 'B', 'B']) dnet.fit(X, y) signature = dnet.get_signature()[0] assert all(x in ('number_of_genes', 'performace_couples', 'features', 'signature', 'score') for x in signature.keys()) assert signature['number_of_genes'] == 2 assert signature['score'] == 0 assert signature['performace_couples'] == 0 assert all(x == y for x, y in zip(signature['features'], (0, 1)))
def test_equal_fit(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) Nprobe, Nsample = (5, 4) X = np.full(shape=(Nsample, Nprobe), fill_value=42.) # y = np.zeros(shape=(Nsample), dtype=int) y = np.array(['A', 'A', 'B', 'B']) with pytest.warns(RuntimeWarning): dnet.fit(X, y) signature = dnet.signatures[0] assert all(x in ('number_of_genes', 'performace_couples', 'features', 'signature', 'score') for x in signature.keys()) assert signature['number_of_genes'] == 2 assert signature['score'] == 0.5 assert signature['performace_couples'] == 2 assert all(x == y for x, y in zip(signature['features'], (0, 1)))
def test_estimator(self): params = { 'estimator': None, 'cv': LeaveOneOut(), 'scoring': None, 'max_chunk': 1, 'percentage': .1, 'verbose': False, 'n_jobs': 1 } with pytest.raises(ValueError): dnet = DNetPRO(**params)
def test_n_jobs(self): params = { 'estimator': GaussianNB(priors=None, var_smoothing=1e-09), 'cv': LeaveOneOut(), 'scoring': None, 'max_chunk': 10, 'percentage': .1, 'verbose': False, 'n_jobs': 0 } with pytest.raises(ValueError): dnet = DNetPRO(**params)
def test_constructor(self): params = { 'estimator': GaussianNB(priors=None, var_smoothing=1e-09), 'cv': LeaveOneOut(), 'scoring': None, 'max_chunk': 100, 'percentage': .1, 'verbose': False, 'n_jobs': 1 } dnet = DNetPRO(**params) print(dnet)
def test_predict_logproba(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) Nprobe, Nsample = (5, 4) X = np.full(shape=(Nsample, Nprobe), fill_value=42.) # y = np.zeros(shape=(Nsample), dtype=int) y = np.array(['A', 'A', 'B', 'B']) with pytest.warns(RuntimeWarning): dnet.fit(X, y) proba = dnet.predict_log_proba(dnet.transform(X)) assert np.isnan(proba).sum() == np.prod(proba.shape)
def test_label2numbers(self): y = ('A', 'A', 'B', 'B') num_y = DNetPRO.label2numbers(y) assert all(x == y for x, y in zip(num_y, (0, 0, 1, 1)))
def test_pendrem_star(self): graph = nx.star_graph(5) g = DNetPRO.pendrem(graph) assert all(x == 0 for x in g.nodes())
n_clusters_per_class=1, class_sep=1., scale=1., shuffle=False, random_state=123) classifier = GaussianNB() # Split the dataset in training and test for performances evaluation X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.33, random_state=42) # Create the DNetPRO feature selection object with GaussianNB classifier dnet = DNetPRO(estimator=classifier, scoring='accuracy', n_jobs=4, verbose=False) # extract the filtered dataset as the signature with highest score in the training set Dnet_data = dnet.fit_transform(X_train, y_train) new_sample, new_probe = Dnet_data.shape # Best DNetPRO signature dnet_signature = dnet.selected_signature # print some informations print('Signature DNetPRO: {}'.format(sorted(dnet_signature))) print('DNetPRO score: {:.3f}'.format(dnet.score(X_test, y_test))) print('Informative found: {:d} / {:d}'.format( len([x for x in dnet_signature if x < Ninformative]), Ninformative)) # Compare the obtained results against the Kbest features with K=number of feature in the DNetPRO signature
def test_pendrem_path(self): graph = nx.path_graph(5) g = DNetPRO.pendrem(graph) assert all(x == y for x, y in zip(g.nodes(), graph.nodes))
def test_is_fitted(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) with pytest.raises(NotFittedError): pred_label = dnet.predict([]) assert pred_label is not None
def test_estimator_type(self): dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) assert dnet._estimator_type == 'classifier'
def test_pendrem_full(self): graph = nx.complete_graph(5) g = DNetPRO.pendrem(graph) assert all(x == y for x, y in zip(g.nodes(), graph.nodes))
n_clusters_per_class=1, class_sep=1., scale=1., shuffle=False, random_state=seed) X_train, X_test, y_train, y_test = train_test_split( data, label, test_size=0.33, random_state=42) # dnet_tic = now() # DNetPRO feature selection dnet = DNetPRO(estimator=classifier, scoring='accuracy', n_jobs=available_cores, verbose=False, max_chunk=10) # Dnet_data = dnet.fit_transform(X_train, y_train) # new_sample, new_probe = Dnet_data.shape # dnet_signature = dnet.selected_signature # # DNetPRO parameters to save # dnet_score = dnet.score(X_test, y_test) # dnet_informative = len([x for x in dnet_signature if x < Ninformative]) # dnet_size = len(dnet_signature) # dnet_toc = now()
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np from DNetPRO import DNetPRO from sklearn.naive_bayes import GaussianNB if __name__ == '__main__': Nprobe, Nsample = (5, 4) X = np.random.uniform(low=0., high=1., size=(Nsample, Nprobe)) y = np.array(['A', 'A', 'B', 'B']) dnet = DNetPRO(estimator=GaussianNB(), max_chunk=4) dnet.fit(X, y) print(dnet.signatures)