-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
205 additions
and
68 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.2.6 | ||
1.2.8 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .graph import * |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from IPython.display import HTML, display | ||
from pandas.core.frame import DataFrame | ||
from pyvis.network import Network | ||
|
||
from likelihood.tools import FeatureSelection | ||
|
||
|
||
class DynamicGraph(FeatureSelection): | ||
"""A class to represent a dynamic graph""" | ||
|
||
def __init__(self, df: DataFrame, n_importances: int): | ||
self.G = Network( | ||
notebook=True, cdn_resources="remote", directed=True | ||
) # enable interactive visualization in Jupyter Notebooks | ||
self.df = df | ||
self.n_importances = n_importances | ||
super().__init__() | ||
|
||
def fit(self, **kwargs) -> None: | ||
"""Fit the model according to the given data and parameters.""" | ||
self.get_digraph(self.df, self.n_importances) | ||
# create a dictionary with the indexes and names of the dataframe | ||
self.get_index = dict(zip(self.df.columns, range(len(self.df.columns)))) | ||
self._make_network() | ||
|
||
def _make_network(self) -> None: | ||
"""Create nodes and edges of the network based on feature importance scores""" | ||
self._add_nodes() | ||
for i in range(len(self.all_features_imp_graph)): | ||
node = self.all_features_imp_graph[i][0] | ||
edges = self.all_features_imp_graph[i][1] | ||
|
||
for label, weight in edges: | ||
self.G.add_edge(i, self.get_index[label], weight=weight) | ||
|
||
def _add_nodes(self) -> None: | ||
for i in range(len(self.all_features_imp_graph)): | ||
node = self.all_features_imp_graph[i][0] | ||
self.G.add_node(n_id=i, label=node) | ||
|
||
def draw(self, name="graph.html", **kwargs) -> None: | ||
"""Display the network using HTML format""" | ||
spring_length = kwargs["spring_length"] if "spring_length" in kwargs else 500 | ||
node_distance = kwargs["node_distance"] if "node_distance" in kwargs else 100 | ||
self.G.repulsion(node_distance=node_distance, spring_length=spring_length) | ||
self.G.show_buttons(filter_=["physics"]) | ||
self.G.show(name) | ||
|
||
html_file_content = open(name, "r").read() | ||
display(HTML(html_file_content)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .regression import * | ||
from .simulation import * |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from numpy import ndarray | ||
from pandas.core.frame import DataFrame | ||
|
||
from likelihood.tools import FeatureSelection, OneHotEncoder | ||
|
||
# -------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
|
||
class SimulationEngine(FeatureSelection): | ||
|
||
def __init__(self, df: DataFrame, n_importances: int, **kwargs): | ||
|
||
self.df = df | ||
self.n_importances = n_importances | ||
|
||
super().__init__(**kwargs) | ||
|
||
def predict(self, column: str, n: int = None) -> ndarray | list: | ||
|
||
# We assing the entries of the dictionary corresponding to the column | ||
w, quick_encoder, names_cols, dfe = self.w_dict[column] | ||
|
||
df_aux = dfe._df | ||
|
||
if n != None: | ||
df_aux = df_aux.iloc[:n, :] | ||
|
||
y = df_aux.to_numpy() @ w | ||
|
||
if quick_encoder != None: | ||
|
||
one_hot = OneHotEncoder() | ||
y = one_hot.decode(y) | ||
encoding_dic = quick_encoder.decoding_list[0] | ||
y = [encoding_dic[item] for item in y] | ||
|
||
return y | ||
|
||
def fit(self, **kwargs) -> None: | ||
|
||
# We run the feature selection algorithm | ||
self.get_digraph(self.df, self.n_importances) |
Oops, something went wrong.