Create helper function to compute norm along axis

This commit is contained in:
Johannes Schönberger
2015-12-04 23:11:19 -05:00
parent f88694e5a3
commit 50f2a0b4ed
+8 -2
View File
@@ -15,6 +15,11 @@ def _check_data_atleast_2D(data):
raise ValueError('Input data must be at least 2D.')
def _norm_along_axis(x, axis):
"""NumPy < 1.8 does not support the `axis` argument for `np.linalg.norm`."""
return np.sqrt(np.einsum('ij,ij->i', x, x))
class BaseModel(object):
def __init__(self):
@@ -239,8 +244,9 @@ class LineModelND(BaseModel):
"""
X0, u = self.params
return np.linalg.norm((data - X0) -
np.dot(data - X0, u)[..., np.newaxis] * u, axis=1)
return _norm_along_axis((data - X0) -
np.dot(data - X0, u)[..., np.newaxis] * u,
axis=1)
def predict(self, x, axis=0, params=None):
"""Predict intersection of the estimated line model with a hyperplane