Created
September 5, 2023 01:35
-
-
Save micahmelling/7c6aee9c932ec74d0b812f1621547068 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import networkx as nx | |
from sklearn.base import BaseEstimator, TransformerMixin | |
class CentralityEncoder(BaseEstimator, TransformerMixin): | |
def __init__(self, node_column, edge_column): | |
self.mapping_dict = {} | |
self.node_column = node_column | |
self.edge_column = edge_column | |
def fit(self, X, Y=None): | |
G = nx.from_pandas_edgelist(X, self.node_column, self.edge_column) | |
centrality = nx.eigenvector_centrality(G) | |
self.mapping_dict.update(centrality) | |
return self | |
def transform(self, X, Y=None): | |
X[self.node_column] = X[self.node_column].map(self.mapping_dict) | |
X[self.edge_column] = X[self.edge_column].map(self.mapping_dict) | |
return X | |
df = pd.DataFrame({ | |
'pass': [1, 0, 0, 1, 1, 1, 1, 0, 1, 1], | |
'teacher': ['a', 'a', 'b', 'a', 'b', 'b', 'c', 'c', 'a', 'd'], | |
'advisor': ['b', 'c', 'c', 'a', 'd', 'd', 'a', 'b', 'a', 'c'] | |
}) | |
print(df) | |
df = CentralityEncoder(node_column='teacher', edge_column='advisor').fit_transform(df) | |
print(df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment