Skip to content

Commit

Permalink
nngraph: standardize instead of center and rescale
Browse files Browse the repository at this point in the history
Rescaling was useful before the adaptive setting (to the mean of the
distances) of the kernel width. It is now useless. Centering can be
combined with normalizing to unit variance to standardize the data.
  • Loading branch information
mdeff committed Feb 15, 2019
1 parent e1879ee commit bf7427f
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 47 deletions.
4 changes: 1 addition & 3 deletions pygsp/graphs/nngraphs/bunny.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,5 @@ def __init__(self, **kwargs):
'distance': 8,
}

super(Bunny, self).__init__(data['bunny'],
center=False, rescale=False,
kind='radius', radius=0.02,
super(Bunny, self).__init__(data['bunny'], kind='radius', radius=0.02,
plotting=plotting, **kwargs)
4 changes: 1 addition & 3 deletions pygsp/graphs/nngraphs/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ def __init__(self,
'distance': 9,
}

super(Cube, self).__init__(pts, k=10,
center=False, rescale=False,
plotting=plotting, **kwargs)
super(Cube, self).__init__(pts, k=10, plotting=plotting, **kwargs)

def _get_extra_repr(self):
attrs = {'radius': '{:.2f}'.format(self.radius),
Expand Down
35 changes: 10 additions & 25 deletions pygsp/graphs/nngraphs/nngraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,6 @@ def _radius_flann(features, radius, metric, order):
}


def center_features(features):
return features - np.mean(features, axis=0)


def rescale_features(features):
n_vertices, dimensionality = features.shape
bounding_radius = 0.5 * np.linalg.norm(np.amax(features, axis=0) -
np.amin(features, axis=0), 2)
scale = np.power(n_vertices, 1 / min(dimensionality, 3)) / 10
return features * scale / bounding_radius


class NNGraph(Graph):
r"""Nearest-neighbor graph.
Expand All @@ -244,10 +232,9 @@ class NNGraph(Graph):
features : ndarray
An `N`-by-`d` matrix, where `N` is the number of nodes in the graph and
`d` is the number of features.
center : bool, optional
Whether to center the features to have zero mean.
rescale : bool, optional
Whether to scale the features so that they lie in an l2-ball.
standardize : bool, optional
Whether to rescale the features so that each feature has a mean of 0
and standard deviation of 1 (unit variance).
metric : {'euclidean', 'manhattan', 'minkowski', 'max_dist'}, optional
Metric used to compute pairwise distances.
order : float, optional
Expand Down Expand Up @@ -290,7 +277,7 @@ class NNGraph(Graph):
"""

def __init__(self, features, center=True, rescale=True,
def __init__(self, features, standardize=False,
metric='euclidean', order=3,
kind='knn', k=10, radius=0.01,
kernel_width=None,
Expand Down Expand Up @@ -318,10 +305,10 @@ def __init__(self, features, center=True, rescale=True,
if kind == 'radius' and radius is not None and radius <= 0:
raise ValueError('The radius must be greater than 0.')

if center:
features = center_features(features)
if rescale:
features = rescale_features(features)
if standardize:
# Don't alter the original data (users would be surprised).
features = features - np.mean(features, axis=0)
features /= np.std(features, axis=0)

nn_function = _nn_functions[kind][backend]
if kind == 'knn':
Expand Down Expand Up @@ -373,8 +360,7 @@ def __init__(self, features, center=True, rescale=True,
W = utils.symmetrize(W, method='average')

# features is stored in coords, potentially standardized
self.center = center
self.rescale = rescale
self.standardize = standardize
self.metric = metric
self.order = order
self.kind = kind
Expand All @@ -387,8 +373,7 @@ def __init__(self, features, center=True, rescale=True,

def _get_extra_repr(self):
return {
'center': self.center,
'rescale': self.rescale,
'standardize': self.standardize,
'metric': self.metric,
'order': self.order,
'kind': self.kind,
Expand Down
4 changes: 1 addition & 3 deletions pygsp/graphs/nngraphs/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def __init__(self, N=64, k=6, distributed=False, seed=None, **kwargs):

coords = rs.uniform(0, 1, (N, 2))

super(Sensor, self).__init__(coords, k=k,
rescale=False, center=False,
plotting=plotting, **kwargs)
super(Sensor, self).__init__(coords, k=k, plotting=plotting, **kwargs)

def _get_extra_repr(self):
return {'k': self.k,
Expand Down
4 changes: 1 addition & 3 deletions pygsp/graphs/nngraphs/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def __init__(self,
'vertex_size': 80,
}

super(Sphere, self).__init__(pts, k=10,
center=False, rescale=False,
plotting=plotting, **kwargs)
super(Sphere, self).__init__(pts, k=10, plotting=plotting, **kwargs)

def _get_extra_repr(self):
attrs = {'radius': '{:.2f}'.format(self.radius),
Expand Down
1 change: 0 additions & 1 deletion pygsp/graphs/nngraphs/twomoons.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def __init__(self, moontype='standard', dim=2, kernel_width=0.05,
}

super(TwoMoons, self).__init__(coords, kernel_width=kernel_width, k=5,
center=False, rescale=False,
plotting=plotting, **kwargs)

def _get_extra_repr(self):
Expand Down
13 changes: 4 additions & 9 deletions pygsp/tests/test_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,10 @@ def test_nngraph(self, n_vertices=30):
self.assertRaises(ValueError, graphs.NNGraph, features,
kind=kind, backend=backend)
else:
graphs.NNGraph(features, metric=metric, kind=kind,
backend=backend,
center=False)
graphs.NNGraph(features, metric=metric,
kind=kind, backend=backend,
rescale=False)
graphs.NNGraph(features, metric=metric,
kind=kind, backend=backend,
center=False, rescale=False)
for standardize in [True, False]:
graphs.NNGraph(features, standardize=standardize,
metric=metric, kind=kind,
backend=backend)

# Invalid parameters.
self.assertRaises(ValueError, graphs.NNGraph, features,
Expand Down

0 comments on commit bf7427f

Please sign in to comment.