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

Fit transform #145

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
be2ee12
gitignore VScode
tomlincr Aug 10, 2023
9c91571
gitignore Mac clutter
tomlincr Aug 10, 2023
aea8c2a
add fit_transform & fit_predict
tomlincr Aug 10, 2023
19257f5
add fit_transform
tomlincr Aug 10, 2023
9f338aa
y=None consistency
tomlincr Aug 10, 2023
272e52a
docs
tomlincr Aug 10, 2023
af71588
add deep=True argument to get_params
tomlincr Aug 10, 2023
cfe8a30
add y passthrough
tomlincr Aug 10, 2023
1ba2a46
test fit_transform
tomlincr Aug 10, 2023
c078a43
add fit_transform to all neighbourhood methods
tomlincr Aug 10, 2023
4b779f8
fix self.embedding
tomlincr Aug 10, 2023
7d49703
assert equal shape for stochastic methods
tomlincr Aug 10, 2023
782bdbd
list -> array
tomlincr Aug 10, 2023
d8fd406
structural fit_transform + tests
tomlincr Aug 10, 2023
7272e25
meta fit_transform
tomlincr Aug 10, 2023
95d1423
meta tests
tomlincr Aug 10, 2023
c039fbb
attributed fit_transform
tomlincr Aug 10, 2023
82de7de
Revert "attributed fit_transform"
tomlincr Aug 10, 2023
99dcb9f
attributed POC
tomlincr Aug 10, 2023
b7e8148
attributed fit_transform method
tomlincr Aug 10, 2023
276e614
attributed fit_transform test
tomlincr Aug 10, 2023
edaadd1
fix sine test
tomlincr Aug 10, 2023
bae0046
try assert equal shape for fails
tomlincr Aug 10, 2023
877f66f
test y arg of fit_transform
tomlincr Aug 11, 2023
8cdf885
test y arg for fit_transform
tomlincr Aug 11, 2023
a3c89d8
test y arg for fit_transform neighbourhood
tomlincr Aug 11, 2023
8092ed0
test y arg for fit_transform attributed
tomlincr Aug 11, 2023
3b7052e
add model to fit_transform
tomlincr Aug 11, 2023
c7ceb75
test META model, not nested estimator
tomlincr Aug 11, 2023
45e12b5
add VSCode + Mac to gitignore
tomlincr Aug 11, 2023
b327689
add set_params to estimator
tomlincr Aug 11, 2023
45e833e
test set_params
tomlincr Aug 11, 2023
90bc37b
Merge pull request #3 from tomlincr/set_params
tomlincr Aug 11, 2023
716a796
Merge branch 'dev' into fit_transform
tomlincr Aug 11, 2023
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,9 @@ dmypy.json

# Pyre type checker
.pyre/

# VSCode project settings
.vscode/

# Mac OS X clutter
**/.DS_Store
19 changes: 17 additions & 2 deletions karateclub/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,36 @@ def get_embedding(self):
"""Getting the embeddings (graph or node level)."""
pass

def fit_transform(self):
"""Fitting a model and getting the embeddings."""
pass

def get_memberships(self):
"""Getting the membership dictionary."""
pass

def fit_predict(self):
"""Fitting a model and getting the membership."""
pass

def get_cluster_centers(self):
"""Getting the cluster centers."""
pass

def get_params(self):

def get_params(self, deep=True):
# Ignore deep=False, since we don't have nested estimators
"""Get parameter dictionary for this estimator.."""
rx = re.compile(r'^\_')
params = self.__dict__
params = {key: params[key] for key in params if not rx.search(key)}
return params

def set_params(self, **parameters):
"""Set the parameters of this estimator."""
for parameter, value in parameters.items():
setattr(self, parameter, value)
return self

def _set_seed(self):
"""Creating the initial random seed."""
random.seed(self.seed)
Expand Down
20 changes: 20 additions & 0 deletions karateclub/node_embedding/attributed/ae.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,23 @@ def get_embedding(self) -> np.array:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
return np.concatenate(self._embeddings, axis=1)

def fit_transform(self,
graph: nx.classes.graph.Graph,
X: Union[np.array, coo_matrix],
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **X** *(Scipy COO array)* - The binary matrix of node features.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, X)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
21 changes: 21 additions & 0 deletions karateclub/node_embedding/attributed/asne.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random
import numpy as np
import networkx as nx
from typing import Union
from scipy.sparse import coo_matrix
from karateclub.estimator import Estimator
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
Expand Down Expand Up @@ -91,3 +92,23 @@ def get_embedding(self) -> np.array:
"""
embedding = self._embedding
return embedding

def fit_transform(self,
graph: nx.classes.graph.Graph,
X: Union[np.array, coo_matrix],
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **X** *(Scipy COO array)* - The binary matrix of node features.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, X)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
20 changes: 20 additions & 0 deletions karateclub/node_embedding/attributed/bane.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,23 @@ def get_embedding(self) -> np.array:
"""
embedding = self._B
return embedding

def fit_transform(self,
graph: nx.classes.graph.Graph,
X: Union[np.array, coo_matrix],
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **X** *(Scipy COO array)* - The binary matrix of node features.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, X)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
20 changes: 20 additions & 0 deletions karateclub/node_embedding/attributed/feathernode.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,23 @@ def get_embedding(self) -> np.array:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
return self._feature_blocks

def fit_transform(self,
graph: nx.classes.graph.Graph,
X: Union[np.array, coo_matrix],
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **X** *(Scipy COO array)* - The binary matrix of node features.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, X)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
20 changes: 20 additions & 0 deletions karateclub/node_embedding/attributed/fscnmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,23 @@ def get_embedding(self) -> np.array:
"""
embedding = np.concatenate([self._B_1, self._U], axis=1)
return embedding

def fit_transform(self,
graph: nx.classes.graph.Graph,
X: Union[np.array, coo_matrix],
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **X** *(Scipy COO array)* - The binary matrix of node features.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, X)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
20 changes: 20 additions & 0 deletions karateclub/node_embedding/attributed/musae.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,23 @@ def get_embedding(self) -> np.array:
"""
embedding = np.concatenate(self.embeddings, axis=1)
return embedding

def fit_transform(self,
graph: nx.classes.graph.Graph,
X: Union[np.array, coo_matrix],
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **X** *(Scipy COO array)* - The binary matrix of node features.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, X)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
20 changes: 20 additions & 0 deletions karateclub/node_embedding/attributed/sine.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ def get_embedding(self) -> np.array:
"""
embedding = self.embedding
return embedding

def fit_transform(self,
graph: nx.classes.graph.Graph,
X: Union[np.array, coo_matrix],
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **X** *(Scipy COO array)* - The binary matrix of node features.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, X)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
20 changes: 20 additions & 0 deletions karateclub/node_embedding/attributed/tadw.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,23 @@ def get_embedding(self) -> np.array:
[np.transpose(self._W), np.transpose(np.dot(self._H, self._T))], axis=1
)
return embedding

def fit_transform(self,
graph: nx.classes.graph.Graph,
X: Union[np.array, coo_matrix],
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **X** *(Scipy COO array)* - The binary matrix of node features.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, X)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
20 changes: 20 additions & 0 deletions karateclub/node_embedding/attributed/tene.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,23 @@ def get_embedding(self) -> np.array:
"""
embedding = np.concatenate([self._M, self._Q], axis=1)
return embedding

def fit_transform(self,
graph: nx.classes.graph.Graph,
T: Union[np.array, coo_matrix],
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **X** *(Scipy COO array)* - The binary matrix of node features.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, T)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
20 changes: 20 additions & 0 deletions karateclub/node_embedding/meta/neu.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,23 @@ def get_embedding(self) -> np.array:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
return self._embedding

def fit_transform(self,
graph: nx.classes.graph.Graph,
model: Estimator,
y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **model** *(KC embedding model)* - Karate Club embedding.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph, model)
if y is None:
return self._embedding
else:
return self._embedding, y
16 changes: 16 additions & 0 deletions karateclub/node_embedding/neighbourhood/boostne.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,19 @@ def get_embedding(self) -> np.array:
"""
embedding = np.concatenate(self._embeddings, axis=1)
return embedding

def fit_transform(self, graph: nx.classes.graph.Graph, y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
16 changes: 16 additions & 0 deletions karateclub/node_embedding/neighbourhood/deepwalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ def get_embedding(self) -> np.array:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
return np.array(self._embedding)

def fit_transform(self, graph: nx.classes.graph.Graph, y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
16 changes: 16 additions & 0 deletions karateclub/node_embedding/neighbourhood/diff2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,19 @@ def get_embedding(self) -> np.array:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
return np.array(self._embedding)

def fit_transform(self, graph: nx.classes.graph.Graph, y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph)
if y is None:
return self.get_embedding()
else:
return self.get_embedding(), y
18 changes: 17 additions & 1 deletion karateclub/node_embedding/neighbourhood/first_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,20 @@ def get_embedding(self) -> np.array:
Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
return self.embedding
return self.embedding

def fit_transform(self, graph: nx.classes.graph.Graph, y=None) -> np.array:
r"""Fits model to input graph and returns embeddings.

Arg types:
* **graph** *(NetworkX graph)* - The graph to be embedded.
* **y** *(None)* - Not used. For consistency with scikit-learn API.

Return types:
* **embedding** *(Numpy array)* - The embedding of nodes.
"""
self.fit(graph)
if y is None:
return self.embedding
else:
return self.embedding, y
Loading
Loading