def test_fit_reg_squared_l1(): clf = CDRegressor(C=1.0, random_state=0, penalty="l1", loss="squared", max_iter=100) clf.fit(digit.data, digit.target) y_pred = (clf.predict(digit.data) > 0.5).astype(int) acc = np.mean(digit.target == y_pred) assert_almost_equal(acc, 1.0, 3)
def test_fit_reg_squared_l1(): clf = CDRegressor(C=1.0, random_state=0, penalty="l1", loss="squared", max_iter=100) clf.fit(digit.data, digit.target) y_pred = (clf.predict(digit.data) > 0.5).astype(int) acc = np.mean(digit.target == y_pred) assert_almost_equal(acc, 1.0, 3)
def test_fit_reg_squared_loss_nn_l2(): K = pairwise_kernels(digit.data, metric="poly", degree=4) clf = CDRegressor(C=1, random_state=0, penalty="nnl2", loss="squared", max_iter=100) clf.fit(K, digit.target) y_pred = (clf.predict(K) > 0.5).astype(int) acc = np.mean(digit.target == y_pred) assert_almost_equal(acc, 0.9444, 3)
def test_fit_reg_squared_multiple_outputs(): reg = CDRegressor(C=0.05, random_state=0, penalty="l1/l2", loss="squared", max_iter=100) lb = LabelBinarizer() Y = lb.fit_transform(mult_target) reg.fit(mult_dense, Y) y_pred = lb.inverse_transform(reg.predict(mult_dense)) assert_almost_equal(np.mean(y_pred == mult_target), 0.797, 3) assert_almost_equal(reg.n_nonzero(percentage=True), 0.5)
def test_fit_reg_squared_multiple_outputs(): reg = CDRegressor(C=1.0, random_state=0, penalty="l2", loss="squared", max_iter=100) Y = np.zeros((len(digit.target), 2)) Y[:, 0] = digit.target Y[:, 1] = digit.target reg.fit(digit.data, Y) y_pred = reg.predict(digit.data) assert_equal(y_pred.shape[0], len(digit.target)) assert_equal(y_pred.shape[1], 2)
def test_fit_reg_squared_multiple_outputs(): reg = CDRegressor(C=1.0, random_state=0, penalty="l2", loss="squared", max_iter=100) Y = np.zeros((len(digit.target), 2)) Y[:, 0] = digit.target Y[:, 1] = digit.target reg.fit(digit.data, Y) y_pred = reg.predict(digit.data) assert_equal(y_pred.shape[0], len(digit.target)) assert_equal(y_pred.shape[1], 2)
def test_fit_reg_squared_l1(train_data): X, y = train_data clf = CDRegressor(C=1.0, random_state=0, penalty="l1", loss="squared", max_iter=100) clf.fit(X, y) y_pred = (clf.predict(X) > 0.5).astype(int) acc = np.mean(y == y_pred) np.testing.assert_almost_equal(acc, 1.0, 3)
def test_fit_reg_squared_loss_nn_l2(): K = pairwise_kernels(digit.data, metric="poly", degree=4) clf = CDRegressor(C=1, random_state=0, penalty="nnl2", loss="squared", max_iter=100) clf.fit(K, digit.target) y_pred = (clf.predict(K) > 0.5).astype(int) acc = np.mean(digit.target == y_pred) assert_almost_equal(acc, 0.9444, 3)
def test_warm_start_l1r_regression(): clf = CDRegressor(warm_start=True, random_state=0, penalty="l1") clf.C = 0.1 clf.fit(bin_dense, bin_target) n_nz = clf.n_nonzero() clf.C = 0.2 clf.fit(bin_dense, bin_target) n_nz2 = clf.n_nonzero() assert_true(n_nz < n_nz2)
def test_fit_reg_squared_multiple_outputs(train_data): X, y = train_data reg = CDRegressor(C=1.0, random_state=0, penalty="l2", loss="squared", max_iter=100) Y = np.zeros((len(y), 2)) Y[:, 0] = y Y[:, 1] = y reg.fit(X, Y) y_pred = reg.predict(X) assert y_pred.shape[0] == len(y) assert y_pred.shape[1] == 2
def test_warm_start_l1r_regression(): clf = CDRegressor(warm_start=True, random_state=0, penalty="l1") clf.C = 0.1 clf.fit(bin_dense, bin_target) n_nz = clf.n_nonzero() clf.C = 0.2 clf.fit(bin_dense, bin_target) n_nz2 = clf.n_nonzero() assert_true(n_nz < n_nz2)
def test_fit_reg_squared_multiple_outputs(): reg = CDRegressor(C=0.05, random_state=0, penalty="l1/l2", loss="squared", max_iter=100) lb = LabelBinarizer() Y = lb.fit_transform(mult_target) reg.fit(mult_dense, Y) y_pred = lb.inverse_transform(reg.predict(mult_dense)) assert_almost_equal(np.mean(y_pred == mult_target), 0.797, 3) assert_almost_equal(reg.n_nonzero(percentage=True), 0.5)