def test_ray(): """Test the ray result matches the sequential one """ for n, p in [[100, 10], [1000, 30], [1000, 100]]: X, y = make_classification(n_samples=n, n_features=p, n_informative=int(0.1 * p), n_redundant=0, n_repeated=0, n_classes=2, n_clusters_per_class=1, flip_y=0.1, shift=0.0, scale=1.0, shuffle=False, random_state=123) y = y[:, np.newaxis] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123) bnn = BnnBinaryClassifier(verbose=0).fit(X_train, y_train) M_F, V_F = bnn.logit_posterior(X_test) seq_result = rate(X_test, M_F, V_F) ray_seq_result = RATE_ray(X_test, M_F, V_F, n_jobs=1) ray_par_result = RATE_ray(X_test, M_F, V_F, n_jobs=2) assert np.array_equal(seq_result, ray_seq_result) assert np.array_equal(seq_result, ray_par_result)
def test_input_shapes_classification(): """Input dimensionality should match model's expectation (either from layers passed in constructor or first fit call) """ p = 100 # First check default layer behaviour (p not set until fit) bnn = BnnBinaryClassifier() bnn.fit(np.random.randn(100, p), np.random.randint(2, size=100)) with pytest.raises(ValueError): bnn.fit(np.random.randn(100, p + 1), np.random.randint(2, size=100)) # Now check if layers are provided bnn = BnnBinaryClassifier(layers=network_layers(p, 1)) with pytest.raises(ValueError): bnn.fit(np.random.randn(p + 1, 102), np.random.randint(2, size=100))
def test_var_params_classification(): n, p = 100, 20 bnn = BnnBinaryClassifier(network_layers(p, 1)) bnn.fit(np.random.randn(n, p), np.random.randint(2, size=n)) W_mean, W_var, b = bnn.var_params() assert W_mean.shape[0] == 128 assert W_var.shape[0] == 128 assert b.shape[0] == 1 # Same for r2 in regression # Chech output shapes # Test input shapes error handling # test default constructors # Check label exception raising # Check accuracy metricc
def test_H_classification(): """The shape of H should match the network architecture """ n, p = 100, 20 bnn = BnnBinaryClassifier(network_layers(p, 1)) with pytest.raises(ValueError): H_arr = bnn.H(np.random.randn(n, p)) bnn.fit(np.random.randn(n, p), np.random.randint(2, size=n)) H_arr = bnn.H(np.random.randn(n, p)) assert H_arr.shape == (n, 128)
def test_label_types_classification(): """Binary classifier only accepts bianry labels """ p = 100 # First check default layer behaviour (p not set until fit) bnn = BnnBinaryClassifier() with pytest.raises(ValueError): X_train = np.random.randn(100, p) bnn.fit(X_train, np.random.rand(100)) # Float labels bnn.fit(X_train, np.random.randint( 3, size=100)) # Multiclass classification labels
def test_predictions_classification(): """Binary classifier only predicts 0s and 1s for labels and values in [0,1] for prbabilities. Also checks shapes """ n, p = 100, 10 n_test = 50 n_mc_samples = 15 #(X_train, y_train), (X_test, y_test) = toy_binary_classification_data(n, p) bnn = BnnBinaryClassifier(verbose=0) bnn.fit(np.random.randn(n, p), np.random.randint(2, size=n)) yhat_labels = bnn.predict(np.random.randn(n_test, p)) assert yhat_labels.shape[0] == n_test assert np.sum(yhat_labels == 0) + np.sum(yhat_labels == 1) == n_test yhat_proba = bnn.predict_proba(np.random.randn(n_test, p)) assert np.all([prob >= 0.0 or prob <= 1.0 for prob in yhat_proba]) yhat_labels_samples = bnn.predict_samples(np.random.randn(n_test, p), n_mc_samples) assert yhat_labels_samples.shape == (n_mc_samples, n_test) assert np.all([val in [0, 1] for val in yhat_labels_samples.flat]) yhat_proba_samples = bnn.predict_proba_samples(np.random.randn(n_test, p), n_mc_samples) assert yhat_proba_samples.shape == (n_mc_samples, n_test) assert np.all( [prob >= 0.0 or prob <= 1.0 for prob in yhat_proba_samples.flat])
def test_unfitted_models(): """Calling predict before fit should raise exception """ bnn = BnnBinaryClassifier() with pytest.raises(NotFittedError): bnn.predict(np.random.randn(10, 3)) bnn.predict_proba(np.random.randn(10, 3)) bnn.predict_samples(np.random.randn(10, 3)) bnn.predict_proba_samples(np.random.randn(10, 3)) bnn = BnnScalarRegressor() with pytest.raises(NotFittedError): bnn.predict(np.random.randn(10, 3)) bnn.predict_samples(np.random.randn(10, 3))
out["metadata"]["polynomial_powers"][dict_key].append(pf.powers_[effect_size_mask!=0,:]) out["metadata"]["effect_sizes"][dict_key].append(effect_sizes[effect_size_mask!=0]) # Add decoy variables X = np.hstack([X, np.random.randn(n, p_decoy)]) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=seed) print("Number of class 1: {},\tClass 2:{}".format(y.shape[0]-np.sum(y), np.sum(y))) # # Fit the models and evaluate their test accuracy # # BNN bnn = BnnBinaryClassifier(verbose=0, n_mc_samples=n_mc_samples) bnn.fit(X_train, y_train, **nn_train_args) test_scores = add_test_score("Bayesian neural network", bnn.score(X_test, y_test), dict_key, repeat_idx) nn = get_deterministic_nn(bnn) nn.fit(X_train, y_train, verbose=0, **nn_train_args) test_scores = add_test_score("neural network", nn.evaluate(X_test, y_test)[1], dict_key, repeat_idx) # # Variable importance # # LASSO lasso_starttime = time.time() logreg_l1 = LogisticRegressionCV( Cs=10,