Beispiel #1
0
def test_pca_invalid():
    X = pd.DataFrame([[3, 0, 1, 6],
                      [1, None, 1, 6],
                      [10, 2, 1, 6],
                      [10, 2, 2, np.nan],
                      [None, 2, 2, 5]])
    pca = PCA()
    with pytest.raises(ValueError, match="must be all numeric"):
        pca.fit(X)

    X = pd.DataFrame([[3, 0, 1, 6],
                      ['a', 'b', 'a', 'b'],
                      [10, 2, 1, 6],
                      [10, 2, 2, 23],
                      [0, 2, 2, 5]])
    pca = PCA()
    with pytest.raises(ValueError, match="must be all numeric"):
        pca.fit_transform(X)

    X_ok = pd.DataFrame([[3, 0, 1, 6],
                         [1, 2, 1, 6],
                         [10, 2, 1, 6],
                         [10, 2, 2, 5],
                         [6, 2, 2, 5]])
    pca = PCA()
    pca.fit(X_ok)
    with pytest.raises(ValueError, match="must be all numeric"):
        pca.transform(X)
Beispiel #2
0
def test_pca_array():
    X = np.array([[3, 0, 1, 6], [1, 2, 1, 6], [10, 2, 1, 6], [10, 2, 2, 5],
                  [6, 2, 2, 5]])
    pca = PCA()
    expected_X_t = pd.DataFrame(
        [[3.176246, 1.282616], [4.969987, -0.702976], [-3.954182, 0.429071],
         [-4.079174, -0.252790], [-0.112877, -0.755922]],
        columns=[f"component_{i}" for i in range(2)])
    pca.fit(X)
    X_t = pca.transform(X)
    assert_frame_equal(expected_X_t, X_t.to_dataframe())
Beispiel #3
0
def test_pca_woodwork_custom_overrides_returned_by_components(X_df):
    y = pd.Series([1, 2, 1])
    override_types = [Integer, Double]
    for logical_type in override_types:
        X = ww.DataTable(X_df, logical_types={0: logical_type})
        pca = PCA(n_components=1)
        pca.fit(X)
        transformed = pca.transform(X, y)
        assert isinstance(transformed, ww.DataTable)
        assert transformed.logical_types == {
            'component_0': ww.logical_types.Double
        }