Skip to content

Commit

Permalink
Merge pull request #61 from pmacg/sklearn-dependency
Browse files Browse the repository at this point in the history
Fix sklearn dependency
  • Loading branch information
pmacg authored Jan 9, 2024
2 parents 5b502c3 + 3f92029 commit 4fcaf42
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ echo "|----------------|"
echo "| Running pytest |"
echo "|----------------|"
cd tests || exit
python3 -m pytest || exit
python -m pytest || exit
cd .. || exit
echo ""
echo ""
Expand Down
7 changes: 7 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

Unreleased
----------

**Fixed**

* Update sklearn dependency to scikit-learn

0.4.6 - 2022-01-11
------------------

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
scipy
sklearn
scikit-learn
pytest
furo
numpy
pylint
networkx
networkx<3.0
matplotlib
pandas
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = '0.4.6'
VERSION = '0.5.0'
DESCRIPTION = 'SGTL - Spectral Graph Theory Library'
LONG_DESCRIPTION =\
"This library provides several methods and algorithms relating to spectral graph theory in python.\n\n" \
Expand All @@ -15,7 +15,7 @@
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
packages=find_packages(),
install_requires=["numpy", "scipy", "sklearn"],
install_requires=["numpy", "scipy", "scikit-learn", "networkx<3.0", "matplotlib", "pandas"],
long_description_content_type='text/markdown',

keywords=['python', 'spectral', 'graph', 'algorithms', 'clustering', 'cheeger'],
Expand Down
2 changes: 1 addition & 1 deletion sgtl/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _sweep_set(graph: sgtl.Graph, vector: List[float]) -> Tuple[Set[int], Set[in
vector = degree_matrix.power(-(1 / 2)).dot(vector)

# First, sort the vertices based on their value in the given vector
sorted_vertices = [i for i, _ in sorted(enumerate(vector), key=(lambda y: y[1]))]
sorted_vertices = [i for i, _ in sorted(enumerate(vector), key=lambda y: y[1])]

# Keep track of which edges to add/subtract from the cut each time
edges_to_add = np.ones(num_vertices)
Expand Down
2 changes: 1 addition & 1 deletion sgtl/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def spectral_clustering(graph: sgtl.Graph, num_clusters: int, num_eigenvectors=N
_, eigenvectors = scipy.sparse.linalg.eigsh(laplacian_matrix, num_eigenvectors, which='SM')

# Perform k-means on the eigenvectors to find the clusters
labels = KMeans(n_clusters=num_clusters).fit_predict(eigenvectors)
labels = KMeans(n_clusters=num_clusters, n_init=10).fit_predict(eigenvectors)

# Split the clusters.
clusters = [[] for _ in range(num_clusters)]
Expand Down
8 changes: 4 additions & 4 deletions sgtl/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def volume(self, vertex_set):
:return: The volume of vertex_set
"""
self._check_vert_num(vertex_set)
return sum([self.degrees[v] for v in vertex_set])
return sum(self.degrees[v] for v in vertex_set)

def weight(self,
vertex_set_l: List[int],
Expand Down Expand Up @@ -214,13 +214,13 @@ def weight(self,
# self-loops
if sets_are_equal:
weight_to_remove = raw_weight / 2
weight_to_remove -= sum([self.adjacency_matrix()[i, i] for i in vertex_set_l]) / 2
weight_to_remove -= sum(self.adjacency_matrix()[i, i] for i in vertex_set_l) / 2
elif not check_for_overlap:
weight_to_remove = 0
else:
overlap = set.intersection(set(vertex_set_l), set(vertex_set_r))
weight_to_remove = self.lil_adj_mat[list(overlap)][:, list(overlap)].sum() / 2
weight_to_remove -= sum([self.adjacency_matrix()[i, i] for i in overlap]) / 2
weight_to_remove -= sum(self.adjacency_matrix()[i, i] for i in overlap) / 2

# Return the corrected weight
return raw_weight - weight_to_remove
Expand Down Expand Up @@ -472,7 +472,7 @@ def knn_graph(data, k: int):
:return: An ``sgtl.Graph`` object representing the ``k``-nearest neighbour graph of the input.
"""
# Create the nearest neighbours for each vertex using sklearn
_, neighbours = NearestNeighbors(n_neighbors=(k+1)).fit(data).kneighbors(data)
_, neighbours = NearestNeighbors(n_neighbors=k+1).fit(data).kneighbors(data)

# Now, let's construct the adjacency matrix of the graph iteratively
adj_mat = scipy.sparse.lil_matrix((data.shape[0], data.shape[0]))
Expand Down

0 comments on commit 4fcaf42

Please sign in to comment.