Skip to content

Commit

Permalink
Merge pull request #7 from lukassykora/small-fixes
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
lukassykora authored Jul 19, 2024
2 parents e9b2b1c + 38a500c commit f38367b
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 76 deletions.
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,32 @@ min_desired_confidence = 0.5 # min 0.5
undesired_state = '0'
desired_state = '1'
# Action Rules Mining
action_rules = ActionRules(min_stable_attributes, min_flexible_attributes, min_undesired_support,
min_undesired_confidence, min_desired_support, min_desired_confidence, verbose=False)
action_rules = ActionRules(
min_stable_attributes=min_stable_attributes,
min_flexible_attributes=min_flexible_attributes,
min_undesired_support=min_undesired_support,
min_undesired_confidence=min_undesired_confidence,
min_desired_support=min_desired_support,
min_desired_confidence=min_desired_confidence,
verbose=True
)
# Fit
action_rules.fit(
data,
stable_attributes,
flexible_attributes,
target,
undesired_state,
desired_state,
data=data, # cuDF or Pandas Dataframe
stable_attributes=stable_attributes,
flexible_attributes=flexible_attributes,
target=target,
target_undesired_state=undesired_state,
target_desired_state=desired_state,
use_sparse_matrix=True, # needs SciPy or Cupyx (if use_gpu is True) installed
use_gpu=False, # needs Cupy installed
)
# Print rules
# Example: [(Age: O) ∧ (Sex: M) ∧ (Embarked: S → C)] ⇒ [Survived: 0 → 1], support of undesired part: 1, confidence of undesired part: 1.0, support of desired part: 1, confidence of desired part: 1.0, uplift: 1.0
for action_rule in action_rules.get_rules().get_ar_notation():
print(action_rule)
# Print rules (pretty notation)
# Example: If attribute 'Age' is 'O', attribute 'Sex' is 'M', attribute 'Embarked' value 'S' is changed to 'C', then 'Survived' value '0' is changed to '1 with uplift: 1.0.
for action_rule in action_rules.get_rules().get_pretty_ar_notation():
print(action_rule)
# JSON export
Expand Down
287 changes: 220 additions & 67 deletions notebooks/Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@
"undesired_state = '0'\n",
"desired_state = '1'\n",
"# Action Rules Mining\n",
"action_rules = ActionRules(min_stable_attributes, min_flexible_attributes, min_undesired_support, min_undesired_confidence, min_desired_support,min_desired_confidence, verbose=False)"
"action_rules = ActionRules(\n",
" min_stable_attributes=min_stable_attributes,\n",
" min_flexible_attributes=min_flexible_attributes,\n",
" min_undesired_support=min_undesired_support,\n",
" min_undesired_confidence=min_undesired_confidence,\n",
" min_desired_support=min_desired_support,\n",
" min_desired_confidence=min_desired_confidence,\n",
" verbose=True\n",
")"
]
},
{
Expand All @@ -110,34 +118,11 @@
"metadata": {},
"outputs": [
{
"name": "stdout",
"name": "stderr",
"output_type": "stream",
"text": [
"Fri Jul 12 19:43:29 2024 \n",
"+-----------------------------------------------------------------------------------------+\n",
"| NVIDIA-SMI 555.42.06 Driver Version: 555.42.06 CUDA Version: 12.5 |\n",
"|-----------------------------------------+------------------------+----------------------+\n",
"| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
"| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |\n",
"| | | MIG M. |\n",
"|=========================================+========================+======================|\n",
"| 0 Quadro M4000 Off | 00000000:52:00.0 On | N/A |\n",
"| 55% 62C P5 26W / 120W | 693MiB / 8192MiB | 11% Default |\n",
"| | | N/A |\n",
"+-----------------------------------------+------------------------+----------------------+\n",
" \n",
"+-----------------------------------------------------------------------------------------+\n",
"| Processes: |\n",
"| GPU GI CI PID Type Process name GPU Memory |\n",
"| ID ID Usage |\n",
"|=========================================================================================|\n",
"| 0 N/A N/A 3414 G /usr/lib/xorg/Xorg 260MiB |\n",
"| 0 N/A N/A 3565 G /usr/bin/gnome-shell 196MiB |\n",
"| 0 N/A N/A 4575 G ...ures=SpareRendererForSitePerProcess 12MiB |\n",
"| 0 N/A N/A 5120 G ...seed-version=20240711-180158.427000 204MiB |\n",
"| 0 N/A N/A 11434 G ...erProcess --variations-seed-version 3MiB |\n",
"| 0 N/A N/A 11826 G /home/lukas/anaconda3/bin/python 2MiB |\n",
"+-----------------------------------------------------------------------------------------+\n"
"'nvidia-smi' is not recognized as an internal or external command,\n",
"operable program or batch file.\n"
]
}
],
Expand Down Expand Up @@ -167,16 +152,208 @@
"execution_count": 5,
"id": "2947b116-81fb-4c57-9e04-bbfcc25e36cf",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Maximum number of nodes to check for support:\n",
"_____________________________________________\n",
"80\n",
"\n",
"SUPPORT for: (0,)\n",
"_________________________________________________\n",
"- extended by stable attribute\n",
"undesired state support: 1\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (1,)\n",
"_________________________________________________\n",
"- extended by stable attribute\n",
"undesired state support: 2\n",
"desired state support: 3\n",
"\n",
"SUPPORT for: (0, 2)\n",
"_________________________________________________\n",
"- extended by stable attribute\n",
"undesired state support: 0\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (0, 3)\n",
"_________________________________________________\n",
"- extended by stable attribute\n",
"undesired state support: 1\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (1, 2)\n",
"_________________________________________________\n",
"- extended by stable attribute\n",
"undesired state support: 1\n",
"desired state support: 2\n",
"\n",
"SUPPORT for: (1, 3)\n",
"_________________________________________________\n",
"- extended by stable attribute\n",
"undesired state support: 1\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (0, 3, 4)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (0, 3, 5)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 1\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (0, 3, 6)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (0, 3, 7)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 1\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (1, 2, 4)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 1\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (1, 2, 5)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (1, 2, 6)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 2\n",
"\n",
"SUPPORT for: (1, 2, 7)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 1\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (1, 3, 4)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 1\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (1, 3, 5)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (1, 3, 6)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (1, 3, 7)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 1\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (0, 3, 4, 6)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (0, 3, 4, 7)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (0, 3, 5, 6)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (0, 3, 5, 7)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 1\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (1, 2, 4, 6)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (1, 2, 4, 7)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 1\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (1, 2, 5, 6)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (1, 2, 5, 7)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (1, 3, 4, 6)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (1, 3, 4, 7)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 1\n",
"desired state support: 0\n",
"\n",
"SUPPORT for: (1, 3, 5, 6)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 1\n",
"\n",
"SUPPORT for: (1, 3, 5, 7)\n",
"_________________________________________________\n",
"- extended by flexible attribute\n",
"undesired state support: 0\n",
"desired state support: 0\n",
"\n"
]
}
],
"source": [
"action_rules.fit(\n",
" data, \n",
" stable_attributes, \n",
" flexible_attributes, \n",
" target, \n",
" undesired_state,\n",
" desired_state, \n",
" True,\n",
" data=data,\n",
" stable_attributes=stable_attributes,\n",
" flexible_attributes=flexible_attributes,\n",
" target=target,\n",
" target_undesired_state=undesired_state,\n",
" target_desired_state=desired_state,\n",
" use_sparse_matrix=True,\n",
" use_gpu=False,\n",
")"
]
},
Expand Down Expand Up @@ -433,13 +610,13 @@
"--min_undesired_confidence 0.5 \\\n",
"--min_desired_support 1 \\\n",
"--min_desired_confidence 0.5 \\\n",
"--csv_path 'data.csv' \\\n",
"--stable_attributes 'Sex, Age' \\\n",
"--flexible_attributes 'Class, Embarked' \\\n",
"--target 'Survived' \\\n",
"--undesired_state '0' \\\n",
"--desired_state '1' \\\n",
"--output_json_path 'output.json' \\\n",
"--csv_path \"data.csv\" \\\n",
"--stable_attributes \"Sex, Age\" \\\n",
"--flexible_attributes \"Class, Embarked\" \\\n",
"--target \"Survived\" \\\n",
"--undesired_state \"0\" \\\n",
"--desired_state \"1\" \\\n",
"--output_json_path \"output.json\" \\\n",
"--use_gpu 0"
]
},
Expand Down Expand Up @@ -483,30 +660,6 @@
"for action_rule in output.get_ar_notation():\n",
" print(action_rule)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6330b4ed-ae6b-4226-8abc-f9c95ec986e5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c4e05e0-3457-4de2-92b2-dbaba606ad05",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "62aa619c-c542-4af6-bd5d-2e9d9efa8801",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -525,7 +678,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion src/action_rules/action_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ def df_to_array(
from scipy.sparse import csr_matrix

data = csr_matrix(df.values, dtype=self.np.uint8).T # type: ignore
data.eliminate_zeros()
else:
data = df.to_numpy(dtype=self.np.uint8).T # type: ignore
return data, columns
Expand Down Expand Up @@ -362,6 +361,7 @@ def fit(
print('Maximum number of nodes to check for support:')
print('_____________________________________________')
print(self.count_max_nodes(stable_items_binding, flexible_items_binding))
print('')
stop_list = self.get_stop_list(stable_items_binding, flexible_items_binding)
frames = self.get_split_tables(data, target_items_binding, target, use_gpu, use_sparse_matrix)
undesired_state = columns.index(target + '_<item_target>_' + str(target_undesired_state))
Expand Down
Loading

0 comments on commit f38367b

Please sign in to comment.