Skip to content

Commit fb5edde

Browse files
committed
added comments to examples
1 parent 199d062 commit fb5edde

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

examples/basic_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# load and preprocess the dataset from csv
99
data = Dataset("datasets/Abalone.csv")
1010

11-
# Create a problem:::
11+
# Create a problem
1212
# dimension represents the dimension of the problem;
1313
# features represent the list of features, while transactions depicts the list of transactions
1414
# the following 4 elements represent weights (support, confidence, coverage, shrinkage)

examples/basic_run_with_get_rules.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
from niaarm import Dataset, get_rules
22
from niapy.algorithms.basic import DifferentialEvolution
33

4-
4+
# load dataset
55
data = Dataset("datasets/Abalone.csv")
6+
7+
# initialize the algorithm
68
algo = DifferentialEvolution(
79
population_size=50, differential_weight=0.5, crossover_probability=0.9
810
)
11+
12+
# define metrics to be used in fitness computation
913
metrics = ("support", "confidence")
1014

15+
# mine association rules
1116
res = get_rules(data, algo, metrics, max_iters=30, logging=True)
1217
# or rules, run_time = get_rules(...)
1318

examples/data_squashing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from niaarm.dataset import Dataset
22
from niaarm.preprocessing import squash
33

4-
4+
# load dataset
55
dataset = Dataset("datasets/Abalone.csv")
6+
7+
# squash the dataset with a threshold of 0.9, using Euclidean distance as a similarity measure
68
squashed = squash(dataset, threshold=0.9, similarity="euclidean")
9+
10+
# print the squashed dataset
711
print(squashed)

examples/text_mining.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from niaarm.mine import get_text_rules
44
from niapy.algorithms.basic import ParticleSwarmOptimization
55

6+
# load corpus and extract the documents as a list of strings
67
df = pd.read_json("datasets/text/artm_test_dataset.json", orient="records")
78
documents = df["text"].tolist()
89

10+
# create a Corpus object from the documents (requires nltk's punkt tokenizer and the stopwords list)
911
try:
1012
corpus = Corpus.from_list(documents)
1113
except LookupError:
@@ -15,21 +17,21 @@
1517
nltk.download("stopwords")
1618
corpus = Corpus.from_list(documents)
1719

20+
# the rest is pretty much the same as with the numerical association rules
21+
# 1. Init algorithm
22+
# 2. Define metrics
23+
# 3. Run algorithm
1824
algorithm = ParticleSwarmOptimization(population_size=200, seed=123)
1925
metrics = ("support", "confidence", "aws")
2026
rules, time = get_text_rules(
2127
corpus,
22-
max_terms=5,
28+
max_terms=8,
2329
algorithm=algorithm,
2430
metrics=metrics,
2531
max_evals=10000,
2632
logging=True,
2733
)
2834

29-
if len(rules):
30-
print(rules)
31-
print(f"Run time: {time:.2f}s")
32-
rules.to_csv("output.csv")
33-
else:
34-
print("No rules generated")
35-
print(f"Run time: {time:.2f}s")
35+
print(rules)
36+
print(f"Run time: {time:.2f}s")
37+
rules.to_csv("output.csv")

examples/visualization.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
from niaarm import Dataset, get_rules
33
from niaarm.visualize import hill_slopes
44

5+
# Load dataset and mine rules
56
dataset = Dataset("datasets/Abalone.csv")
67
metrics = ("support", "confidence")
78
rules, _ = get_rules(
89
dataset, "DifferentialEvolution", metrics, max_evals=1000, seed=1234
910
)
11+
12+
# Visualize any rule using the hill_slope function like so:
1013
some_rule = rules[150]
1114
print(some_rule)
1215
fig, ax = hill_slopes(some_rule, dataset.transactions)

0 commit comments

Comments
 (0)