Skip to content

Commit 5bb7e17

Browse files
authored
Merge pull request #2 from J41R0/dev
Update to version 1.0.0
2 parents 4abc7a3 + d710246 commit 5bb7e17

20 files changed

+1937
-545
lines changed

README.md

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
1-
# py-fcm
2-
Fuzzy cognitive maps python library
1+
# PyFCM
2+
Fuzzy cognitive maps python library. Also, supports the topology generation from data to solve classification problems.
3+
The details associated to the generation process are described in [this paper](https://link.springer.com/chapter/10.1007/978-3-030-89691-1_25).
4+
### Installation
35

4-
###Example usage
6+
#### From source:
57

8+
1. Clone repository:
9+
```
10+
$ git clone https://github.com/J41R0/PyFCM.git
11+
$ cd PyFCM
12+
```
13+
2. Install setup tools and package:
14+
```
15+
$ pip install setuptools
16+
$ python setup.py install
17+
```
18+
#### From PyPi:
19+
1. Install package using pip:
20+
```
21+
$ pip install py-fcm
22+
```
23+
24+
### Example usage
25+
26+
#### Inference:
627
```
728
from py_fcm import from_json
829

930
fcm_json = """{
10-
"max_iter": 500,
31+
"max_iter": 500,
1132
"decision_function": "LAST",
1233
"activation_function": "sigmoid",
1334
"memory_influence": False,
@@ -54,6 +75,29 @@ fcm_json = """{
5475
"""
5576
my_fcm = from_json(fcm_json)
5677
my_fcm.run_inference()
57-
result = my_fcm.get_result_by_type(node_type='any')
78+
result = my_fcm.get_final_state(concept_type='any')
5879
print(result)
80+
```
81+
82+
#### Generation:
83+
```
84+
import pandas
85+
from py_fcm import FcmEstimator
86+
87+
data_dict = {
88+
'F1': ['x', 'x', 'y', 'y'],
89+
'F2': [9.8, 7.3, 1.1, 3.6],
90+
'class': ['a', 'a', 'r', 'r']
91+
}
92+
93+
train = pandas.DataFrame(data_dict)
94+
x_train = train.loc[:, train.columns != 'class']
95+
y_train = train.loc[:, 'class']
96+
97+
estimator = FcmEstimator()
98+
estimator.fit(x_train, y_train)
99+
print(estimator.predict(x_train))
100+
print("Accuracy: ",estimator.score(x_train, y_train))
101+
print(estimator.get_fcm().to_json())
102+
59103
```

py_fcm/__init__.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
1-
from py_fcm.fcm import FuzzyCognitiveMap, from_json, join_maps
2-
from py_fcm.__const import TYPE_SIMPLE, TYPE_DECISION, TYPE_FUZZY
1+
from py_fcm.utils.functions import Relation
2+
from py_fcm.fcm_estimator import FcmEstimator
3+
from py_fcm.loader import from_json, join_maps, FuzzyCognitiveMap
4+
from py_fcm.utils.__const import TYPE_SIMPLE, TYPE_DECISION, TYPE_FUZZY
5+
6+
7+
def load_csv_dataset(dataset_dir, ds_name, factorize=False):
8+
import pandas
9+
from collections import OrderedDict
10+
data_dict = OrderedDict()
11+
with open(dataset_dir + "/" + ds_name) as file:
12+
content = []
13+
for line in file:
14+
content.append(line.strip())
15+
# first line describe the atributes names
16+
attributes = str(content[0]).split(',')
17+
for current_att in attributes:
18+
data_dict[current_att] = []
19+
20+
# print(data_dict)
21+
for line in range(1, len(content)):
22+
data_line = str(content[line]).split(',')
23+
# avoid rows with different attributes length
24+
if len(data_line) == len(attributes):
25+
# the missing data must be identified
26+
for data in range(0, len(data_line)):
27+
# reusing for value type inference
28+
current_att = data_line[data]
29+
data_dict[attributes[data]].append(current_att)
30+
else:
31+
# Handle errors in dataset matrix
32+
print("Errors in line: ", line, len(data_line), len(attributes))
33+
# adding data set
34+
print("\n===> Dataset for test: ", ds_name)
35+
try:
36+
dataset_frame = pandas.DataFrame(data_dict).drop_duplicates()
37+
except Exception as err:
38+
for key, value in data_dict.items():
39+
print(key, value)
40+
pass
41+
raise Exception(ds_name + " " + str(err))
42+
# Transform the columns disc values in 0..N values
43+
if factorize:
44+
# for current_col in dataset_frame.
45+
dataset_frame = dataset_frame.apply(lambda x: pandas.factorize(x)[0])
46+
return dataset_frame

0 commit comments

Comments
 (0)