From fcb0d4d229dbb94938a45fbbb8f7bda76d1d7b47 Mon Sep 17 00:00:00 2001 From: daxiongshu Date: Sat, 26 Sep 2020 15:45:39 -0400 Subject: [PATCH 1/5] change np.zeros to np.zeros_like --- dask_glm/algorithms.py | 12 ++++++------ dask_glm/utils.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/dask_glm/algorithms.py b/dask_glm/algorithms.py index e959616..5280724 100644 --- a/dask_glm/algorithms.py +++ b/dask_glm/algorithms.py @@ -97,7 +97,7 @@ def gradient_descent(X, y, max_iter=100, tol=1e-14, family=Logistic, **kwargs): stepSize = 1.0 recalcRate = 10 backtrackMult = firstBacktrackMult - beta = np.zeros(p) + beta = np.zeros_like(X._meta, shape=(p)) for k in range(max_iter): # how necessary is this recalculation? @@ -161,9 +161,9 @@ def newton(X, y, max_iter=50, tol=1e-8, family=Logistic, **kwargs): """ gradient, hessian = family.gradient, family.hessian n, p = X.shape - beta = np.zeros(p) # always init to zeros? + beta = np.zeros_like(X._meta, shape=(p)) Xbeta = dot(X, beta) - + print(Xbeta) iter_count = 0 converged = False @@ -387,7 +387,7 @@ def proximal_grad(X, y, regularizer='l1', lamduh=0.1, family=Logistic, stepSize = 1.0 recalcRate = 10 backtrackMult = firstBacktrackMult - beta = np.zeros(p) + beta = np.zeros_like(X._meta, shape=(p)) regularizer = Regularizer.get(regularizer) for k in range(max_iter): @@ -406,8 +406,8 @@ def proximal_grad(X, y, regularizer='l1', lamduh=0.1, family=Logistic, # Compute the step size lf = func for ii in range(100): - beta = regularizer.proximal_operator(obeta - stepSize * gradient, stepSize * lamduh) - step = obeta - beta + beta = regularizer.proximal_operator(- stepSize * gradient + obeta, stepSize * lamduh) + step = - beta + obeta Xbeta = X.dot(beta) Xbeta, beta = persist(Xbeta, beta) diff --git a/dask_glm/utils.py b/dask_glm/utils.py index a189c6a..86216c7 100644 --- a/dask_glm/utils.py +++ b/dask_glm/utils.py @@ -23,7 +23,7 @@ def normalize_inputs(X, y, *args, **kwargs): raise ValueError('Multiple constant columns detected!') mean[intercept_idx] = 0 std[intercept_idx] = 1 - mean = mean if len(intercept_idx[0]) else np.zeros(mean.shape) + mean = mean if len(intercept_idx[0]) else np.zeros_like(X._meta, shape=mean.shape) Xn = (X - mean) / std out = algo(Xn, y, *args, **kwargs).copy() i_adj = np.sum(out * mean / std) From a247c15357438d837c16066fe7c36abf95ac872c Mon Sep 17 00:00:00 2001 From: daxiongshu Date: Sat, 26 Sep 2020 15:53:30 -0400 Subject: [PATCH 2/5] remove print --- dask_glm/algorithms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dask_glm/algorithms.py b/dask_glm/algorithms.py index 5280724..64eedd7 100644 --- a/dask_glm/algorithms.py +++ b/dask_glm/algorithms.py @@ -163,7 +163,6 @@ def newton(X, y, max_iter=50, tol=1e-8, family=Logistic, **kwargs): n, p = X.shape beta = np.zeros_like(X._meta, shape=(p)) Xbeta = dot(X, beta) - print(Xbeta) iter_count = 0 converged = False From c16925af66eaebca99fced46f35a429e6da0f712 Mon Sep 17 00:00:00 2001 From: daxiongshu Date: Sat, 26 Sep 2020 15:57:33 -0400 Subject: [PATCH 3/5] fix flake8 --- dask_glm/algorithms.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dask_glm/algorithms.py b/dask_glm/algorithms.py index 64eedd7..524b710 100644 --- a/dask_glm/algorithms.py +++ b/dask_glm/algorithms.py @@ -163,6 +163,7 @@ def newton(X, y, max_iter=50, tol=1e-8, family=Logistic, **kwargs): n, p = X.shape beta = np.zeros_like(X._meta, shape=(p)) Xbeta = dot(X, beta) + iter_count = 0 converged = False From 6a369e76330938f680233b7de53adafa319d3d9b Mon Sep 17 00:00:00 2001 From: daxiongshu Date: Fri, 9 Oct 2020 11:41:03 -0400 Subject: [PATCH 4/5] use da.ones_like in add_intercept --- dask_glm/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dask_glm/utils.py b/dask_glm/utils.py index 86216c7..0fe3429 100644 --- a/dask_glm/utils.py +++ b/dask_glm/utils.py @@ -140,7 +140,7 @@ def add_intercept(X): 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)) + o = da.ones_like(X, shape=(X.shape[0], 1), chunks=(j, 1)) if is_dask_array_sparse(X): o = o.map_blocks(sparse.COO) # TODO: Needed this `.rechunk` for the solver to work From 1dcf6ad683e3197dd960475964d5a60a4a535bf7 Mon Sep 17 00:00:00 2001 From: daxiongshu Date: Fri, 9 Oct 2020 12:57:49 -0400 Subject: [PATCH 5/5] remove extra parans --- dask_glm/algorithms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dask_glm/algorithms.py b/dask_glm/algorithms.py index 524b710..1320e7b 100644 --- a/dask_glm/algorithms.py +++ b/dask_glm/algorithms.py @@ -97,7 +97,7 @@ def gradient_descent(X, y, max_iter=100, tol=1e-14, family=Logistic, **kwargs): stepSize = 1.0 recalcRate = 10 backtrackMult = firstBacktrackMult - beta = np.zeros_like(X._meta, shape=(p)) + beta = np.zeros_like(X._meta, shape=p) for k in range(max_iter): # how necessary is this recalculation? @@ -161,7 +161,7 @@ def newton(X, y, max_iter=50, tol=1e-8, family=Logistic, **kwargs): """ gradient, hessian = family.gradient, family.hessian n, p = X.shape - beta = np.zeros_like(X._meta, shape=(p)) + beta = np.zeros_like(X._meta, shape=p) Xbeta = dot(X, beta) iter_count = 0 @@ -387,7 +387,7 @@ def proximal_grad(X, y, regularizer='l1', lamduh=0.1, family=Logistic, stepSize = 1.0 recalcRate = 10 backtrackMult = firstBacktrackMult - beta = np.zeros_like(X._meta, shape=(p)) + beta = np.zeros_like(X._meta, shape=p) regularizer = Regularizer.get(regularizer) for k in range(max_iter):