示例#1
0
 def test_sklearn_compatibility_clf(self):
     clf = DAGClassifier(
         alpha=0.0,
         fit_intercept=True,
         dependent_target=True,
         hidden_layer_units=[0],
         standardize=True,
     )
     clf.get_params(deep=True)
示例#2
0
 def test_class_number_error(self, y):
     clf = DAGClassifier(alpha=0.1)
     X = pd.DataFrame(np.random.normal(size=(100, 2)))
     with pytest.raises(
             ValueError,
             match="This solver needs samples of at least 2 classes"
             " in the data, but the data contains only one"
             " class: 0",
     ):
         clf.fit(X, y)
示例#3
0
    def test_feature_importances_binary(self, hidden_layer_units):
        clf = DAGClassifier(alpha=0.1, hidden_layer_units=hidden_layer_units)
        X, y = (
            pd.DataFrame(np.random.normal(size=(100, 2))),
            pd.Series(np.zeros(shape=(100, ), dtype=int)),
        )
        y[X[0] < 0] = 1
        clf.fit(X, y)

        assert isinstance(clf.feature_importances_, np.ndarray)
        coef_ = pd.DataFrame(clf.feature_importances_, columns=X.columns)
        # assert that the sign of the coefficient is positive for both nonlinear and linear cases
        assert coef_.iloc[0, 0] > 0
示例#4
0
 def test_wrong_target_dist_error(self, target_dist_type):
     with pytest.raises(
             NotImplementedError,
             # match=f"Currently only implements [{', '.join(DAGClassifier._supported_types)}] dist types."
             # " Got: {target_dist_type}",
     ):
         DAGClassifier(target_dist_type=target_dist_type)
示例#5
0
    def test_coef_categorical(self, hidden_layer_units):
        clf = DAGClassifier(alpha=0.1, hidden_layer_units=hidden_layer_units)
        X, y = (
            pd.DataFrame(np.random.normal(size=(100, 2))),
            pd.Series(np.zeros(shape=(100, ), dtype=int)),
        )
        y[X[0] < -0.1] = 1
        y[X[0] > 0.1] = 2
        clf.fit(X, y)

        assert isinstance(clf.coef_, np.ndarray)
        assert clf.coef_.shape == (3, 2)
        coef_ = pd.DataFrame(clf.coef_, columns=X.columns)
        # second category is made likely by negative X
        assert coef_.iloc[1, 0] < 0
        # third category is made likely by positive X
        assert coef_.iloc[2, 0] > 0
示例#6
0
    def test_value_predict_type_binary(self, y_type):
        clf = DAGClassifier(alpha=0.1)
        X, y = (
            pd.DataFrame(np.random.normal(size=(100, 2))),
            pd.Series(np.zeros(shape=(100, ), dtype=y_type)),
        )
        y[X[0] < 0] = y_type(1)
        clf.fit(X, y)

        y_pred = clf.predict(X)
        assert isinstance(y_pred[0], y_type)
        y_pred_proba = clf.predict_proba(X)
        assert isinstance(y_pred_proba[0, 0], np.float64)
        assert len(y_pred_proba.shape) == 2
示例#7
0
 def test_glm(self, target_dist_type, y):
     clf = DAGClassifier(target_dist_type=target_dist_type)
     X = np.random.normal(size=(100, 2))
     clf.fit(X, y)
     clf.predict(X)