Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Allow add_intercept for unknown dims #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions dask_glm/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ def test_add_intercept_dask():
assert_eq(result, expected)


def test_add_intercept_unknown():
dd = pytest.importorskip('dask.dataframe')
X = dd.from_array(da.from_array(np.zeros((4, 4)), chunks=(2, 4))).values
result = utils.add_intercept(X)
expected = da.from_array(np.array([
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 1],
], dtype=X.dtype), chunks=2)
assert_eq(result, expected)

def test_sparse():
sparse = pytest.importorskip('sparse')
from sparse.utils import assert_eq
Expand Down
14 changes: 7 additions & 7 deletions dask_glm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ def add_intercept(X):
return np.concatenate([X, np.ones((X.shape[0], 1))], axis=1)


def _add_intercept_block(x):
o = np.ones((len(x), 1), dtype=x.dtype)
return np.concatenate([x, o], axis=1)


@dispatch(da.Array)
def add_intercept(X):
if np.isnan(np.sum(X.shape)):
raise NotImplementedError("Can not add intercept to array with "
"unknown chunk shape")
j, k = X.chunks
o = da.ones((X.shape[0], 1), chunks=(j, 1))
# TODO: Needed this `.rechunk` for the solver to work
# Is this OK / correct?
X_i = da.concatenate([X, o], axis=1).rechunk((j, (k[0] + 1,)))
k2 = (__builtins__['sum'](k) + 1,)
X_i = X.map_blocks(_add_intercept_block, dtype=X.dtype, chunks=(j, k2))
return X_i


Expand Down