# pyFUME
pyFUME is a Python package for automatic Fuzzy Models Estimation from data [1].
pyFUME contains functions to estimate the antecedent sets and the consequent parameters of a Takagi-Sugeno fuzzy model directly from data. This information is then used to create an executable fuzzy model using the Simpful library.
pyFUME also provides facilities for the evaluation of performance.
For more information about pyFUME's functionalities, please check the [online documentation](https://pyfume.readthedocs.io/en/latest/).
## Usage
For the following example, we use the Concrete Compressive Strength data set [2] as can be found in the UCI repository.
The code in Example 1 is simple and easy to use, making it ideal to use for practitioners who wish to use the default settings or only wish to use few non-default settings using additional input arguments (Example 2).
Users that wish to deviate from the default settings can use the code as shown in Example 3.
The code of the Simpful model that is generated is automatically saved (in the same location as the pyFUME script is ran from) under the name 'Simpful_code.py'
## Note
Please be aware that pyFUME's feature selection functionality makes use of multiprocessing.
When feature selection is used, the main script should always be guarded by including "if \_\_name\_\_ == '\_\_main\_\_':" in the header the script.
When the Spyder IDE is used, one should include "if \_\_name\_\_ == '\_\_main\_\_' and '\_\_file\_\_' in globals():".
### Example 1
```
from pyfume import pyFUME
# Set the path to the data and choose the number of clusters
path='./Concrete_data.csv'
nc=3
# Generate the Takagi-Sugeno FIS
FIS = pyFUME(datapath=path, nr_clus=nc)
# Calculate and print the accuracy of the generated model
MAE=FIS.calculate_error(method="MAE")
print ("The estimated error of the developed model is:", MAE)
## Use the FIS to predict the compressive strength of a new concrete sample
# Extract the model from the FIS object
model=FIS.get_model()
# Set the values for each variable
model.set_variable('Cement', 300.0)
model.set_variable('BlastFurnaceSlag', 50.0)
model.set_variable('FlyAsh', 0.0)
model.set_variable('Water', 175.0)
model.set_variable('Superplasticizer',0.7)
model.set_variable('CoarseAggregate', 900.0)
model.set_variable('FineAggregate', 600.0)
model.set_variable('Age', 45.0)
# Perform inference and print predicted value
print(model.Sugeno_inference(['OUTPUT']))
```
### Example 2
```
from pyfume import pyFUME
# Set the path to the data and choose the number of clusters
path='./Concrete_data.csv'
nc=3
# Generate the Takagi-Sugeno FIS
FIS = pyFUME(datapath=path, nr_clus=nc, feature_selection='fst-pso')
# Calculate and print the accuracy of the generated model
MAE=FIS.calculate_error(method="MAE")
print ("The estimated error of the developed model is:", MAE)
## Use the FIS to predict the compressive strength of a new concrete sample
# Extract the model from the FIS object
model=FIS.get_model()
# Set the values for each variable
model.set_variable('Cement', 300.0)
model.set_variable('BlastFurnaceSlag', 50.0)
model.set_variable('FlyAsh', 0.0)
model.set_variable('Water', 175.0)
model.set_variable('Superplasticizer',0.7)
model.set_variable('CoarseAggregate', 900.0)
model.set_variable('FineAggregate', 600.0)
model.set_variable('Age', 45.0)
# Perform inference and print predicted value
print(model.Sugeno_inference(['OUTPUT']))
```
### Example 3
```
from pyfume import *
# Set the path to the data and choose the number of clusters
path='./Concrete_data.csv'
nr_clus=3
# Load and normalize the data using min-max normalization
dl=DataLoader(path,normalize='minmax')
variable_names=dl.variable_names
dataX=dl.dataX
dataY=dl.dataY
# Split the data using the hold-out method in a training (default: 75%)
# and test set (default: 25%).
ds = DataSplitter()
x_train, y_train, x_test, y_test = ds.holdout(dataX=dl.dataX, dataY=dl.dataY)
# Select features relevant to the problem
fs=FeatureSelector(dataX=x_train, dataY=y_train, nr_clus=nr_clus, variable_names=variable_names)
selected_feature_indices, variable_names=fs.wrapper()
# Adapt the training and test input data after feature selection
x_train = x_train[:, selected_feature_indices]
x_test = x_test[:, selected_feature_indices]
# Cluster the training data (in input-output space) using FCM with default settings
cl = Clusterer(x_train=x_train, y_train=y_train, nr_clus=nr_clus)
cluster_centers, partition_matrix, _ = cl.cluster(method="fcm")
# Estimate the membership funtions of the system (default: mf_shape = gaussian)
ae = AntecedentEstimator(x_train=x_train, partition_matrix=partition_matrix)
antecedent_parameters = ae.determineMF()
# Calculate the firing strength of each rule for each data instance
fsc=FireStrengthCalculator(antecedent_parameters=antecedent_parameters, nr_clus=nr_clus, variable_names=variable_names)
firing_strengths = fsc.calculate_fire_strength(data=x_train)
# Estimate the parameters of the consequent functions
ce = ConsequentEstimator(x_train=x_train, y_train=y_train, firing_strengths=firing_strengths)
consequent_parameters = ce.suglms()
# Build a first-order Takagi-Sugeno model using Simpful. Specify the optional
# 'extreme_values' argument to specify the universe of discourse of the input
# variables if you which to use Simpful's membership function plot functionalities.
simpbuilder = SugenoFISBuilder(antecedent_sets=antecedent_parameters, consequent_parameters=consequent_parameters, variable_names=variable_names)
model = simpbuilder.get_model()
# Calculate the mean squared error (MSE) of the model using the test data set
test=SugenoFISTester(model=model, test_data=x_test, variable_names=variable_names, golden_standard=y_test)
MSE = test.calculate_MSE()
print('The mean squared error of the created model is', MSE)
```
### Example 4
```
from pyfume import pyFUME
import pandas as pd
import numpy as np
# Read a Pandas dataframe (using the Pandas library)
df = pd.read_csv('.\Concrete_data.csv')
# Generate the Takagi-Sugeno FIS
FIS = pyFUME(dataframe=df, nr_clus=2)
# Calculate and print the accuracy of the generated model
MAE=FIS.calculate_error(method="MAE")
print ("The estimated error of the developed model is:", MAE)
### Use the FIS to predict the compressive strength of a new concrete samples
## Using Simpful's syntax (NOTE: This approach ONLY works for models built using non-normalized data!)
# Extract the model from the FIS object
model=FIS.get_model()
# Set the values for each variable
model.set_variable('Cement', 300.0)
model.set_variable('BlastFurnaceSlag', 50.0)
model.set_variable('FlyAsh', 0.0)
model.set_variable('Water', 175.0)
model.set_variable('Superplasticizer',0.7)
model.set_variable('CoarseAggregate', 900.0)
model.set_variable('FineAggregate', 600.0)
model.set_variable('Age', 45.0)
# Perform inference and print predicted value
print('The output using Simpfuls "set_variable" functionality is:', model.Sugeno_inference(['OUTPUT']))
## Using pyFUME's syntax (NOTE: This approach DOES work for models built using normalized data!)
# Create numpy array (matrix) in which each row is a data instance to be processed
new_data_one_instance=np.array([[300, 50,0,175,0.7,900,600,45]])
prediction_labels_one_instance=FIS.predict_label(new_data_one_instance)
print('The output using pyFUMEs "predict_label" functionality is:', prediction_labels_one_instance)
# Example in which output for multiple data instances is computed
new_data_multiple_instances=np.array([[300, 50,0,175,0.7,900,600,45],[500, 75,30,200,0.9,600,760,39],[250, 40,10,175,0.3,840,360,51]])
prediction_labels_multiple_instance=FIS.predict_label(new_data_multiple_instances)
print('The output using pyFUMEs "predict_label" functionality is:', prediction_labels_multiple_instance)
### Plot the actual values vs the predicted values of the test data using the matplotlib library
# Predict the labels of the test data
pred = FIS.predict_test_data()
# Get the actual labels of the test data
_, actual = FIS.get_data(data_set='test')
# Create scatterplot
import matplotlib.pyplot as plt
plt.scatter(actual, pred)
plt.xlabel('Actual value')
plt.ylabel('Predicted value')
plt.plot([0,85],[0,85],'r') # Add a reference line
plt.show()
```
## Installation
`pip install pyfume`
## Further information
If you need further information, please write an e-mail to Caro Fuchs: c.e.m.fuchs(at)tue.nl.
## References
[1] Fuchs, C., Spolaor, S., Nobile, M. S., & Kaymak, U. (2020) "pyFUME: a Python package for fuzzy model estimation". In 2020 IEEE International Conference on Fuzzy Systems (FUZZ-IEEE) (pp. 1-8). IEEE.
[2] I-Cheng Yeh, "Modeling of strength of high performance concrete using artificial neural networks," Cement and Concrete Research, Vol. 28, No. 12, pp. 1797-1808 (1998). http://archive.ics.uci.edu/ml/datasets/Concrete+Compressive+Strength
Raw data
{
"_id": null,
"home_page": "https://github.com/CaroFuchs/pyFUME",
"name": "pyFUME",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "fuzzy logic, fuzzy inference systems, fuzzy model, data-driven, model estimation, machine learning",
"author": "Caro Fuchs",
"author_email": "c.e.m.fuchs@tue.nl",
"download_url": "https://files.pythonhosted.org/packages/43/55/3ca6936da251cb56e0a1de1249c25cde6bf89b2d36ceab7cfe12fe4053ad/pyfume-0.3.4.tar.gz",
"platform": null,
"description": "# pyFUME\n\npyFUME is a Python package for automatic Fuzzy Models Estimation from data [1].\npyFUME contains functions to estimate the antecedent sets and the consequent parameters of a Takagi-Sugeno fuzzy model directly from data. This information is then used to create an executable fuzzy model using the Simpful library.\npyFUME also provides facilities for the evaluation of performance.\nFor more information about pyFUME's functionalities, please check the [online documentation](https://pyfume.readthedocs.io/en/latest/).\n\n## Usage\nFor the following example, we use the Concrete Compressive Strength data set [2] as can be found in the UCI repository.\nThe code in Example 1 is simple and easy to use, making it ideal to use for practitioners who wish to use the default settings or only wish to use few non-default settings using additional input arguments (Example 2). \nUsers that wish to deviate from the default settings can use the code as shown in Example 3.\nThe code of the Simpful model that is generated is automatically saved (in the same location as the pyFUME script is ran from) under the name 'Simpful_code.py'\n\n## Note\nPlease be aware that pyFUME's feature selection functionality makes use of multiprocessing. \nWhen feature selection is used, the main script should always be guarded by including \"if \\_\\_name\\_\\_ == '\\_\\_main\\_\\_':\" in the header the script.\nWhen the Spyder IDE is used, one should include \"if \\_\\_name\\_\\_ == '\\_\\_main\\_\\_' and '\\_\\_file\\_\\_' in globals():\".\n\n### Example 1\n```\nfrom pyfume import pyFUME\n\n# Set the path to the data and choose the number of clusters\npath='./Concrete_data.csv'\nnc=3\n\n# Generate the Takagi-Sugeno FIS\nFIS = pyFUME(datapath=path, nr_clus=nc)\n\n# Calculate and print the accuracy of the generated model\nMAE=FIS.calculate_error(method=\"MAE\")\nprint (\"The estimated error of the developed model is:\", MAE)\n\n## Use the FIS to predict the compressive strength of a new concrete sample\n# Extract the model from the FIS object\nmodel=FIS.get_model()\n\n# Set the values for each variable\nmodel.set_variable('Cement', 300.0)\nmodel.set_variable('BlastFurnaceSlag', 50.0)\nmodel.set_variable('FlyAsh', 0.0)\nmodel.set_variable('Water', 175.0)\nmodel.set_variable('Superplasticizer',0.7)\nmodel.set_variable('CoarseAggregate', 900.0)\nmodel.set_variable('FineAggregate', 600.0)\nmodel.set_variable('Age', 45.0)\n\n# Perform inference and print predicted value\nprint(model.Sugeno_inference(['OUTPUT']))\n```\n\n### Example 2\n```\nfrom pyfume import pyFUME\n\n# Set the path to the data and choose the number of clusters\npath='./Concrete_data.csv'\nnc=3\n\n# Generate the Takagi-Sugeno FIS\nFIS = pyFUME(datapath=path, nr_clus=nc, feature_selection='fst-pso')\n\n# Calculate and print the accuracy of the generated model\nMAE=FIS.calculate_error(method=\"MAE\")\nprint (\"The estimated error of the developed model is:\", MAE)\n\n## Use the FIS to predict the compressive strength of a new concrete sample\n# Extract the model from the FIS object\nmodel=FIS.get_model()\n\n# Set the values for each variable\nmodel.set_variable('Cement', 300.0)\nmodel.set_variable('BlastFurnaceSlag', 50.0)\nmodel.set_variable('FlyAsh', 0.0)\nmodel.set_variable('Water', 175.0)\nmodel.set_variable('Superplasticizer',0.7)\nmodel.set_variable('CoarseAggregate', 900.0)\nmodel.set_variable('FineAggregate', 600.0)\nmodel.set_variable('Age', 45.0)\n\n# Perform inference and print predicted value\nprint(model.Sugeno_inference(['OUTPUT']))\n```\n\n### Example 3\n\n```\nfrom pyfume import *\n\n# Set the path to the data and choose the number of clusters\npath='./Concrete_data.csv'\nnr_clus=3\n\n# Load and normalize the data using min-max normalization\ndl=DataLoader(path,normalize='minmax')\nvariable_names=dl.variable_names \ndataX=dl.dataX\ndataY=dl.dataY\n\n# Split the data using the hold-out method in a training (default: 75%) \n# and test set (default: 25%).\nds = DataSplitter()\nx_train, y_train, x_test, y_test = ds.holdout(dataX=dl.dataX, dataY=dl.dataY)\n\n# Select features relevant to the problem\nfs=FeatureSelector(dataX=x_train, dataY=y_train, nr_clus=nr_clus, variable_names=variable_names)\nselected_feature_indices, variable_names=fs.wrapper()\n\n# Adapt the training and test input data after feature selection\nx_train = x_train[:, selected_feature_indices]\nx_test = x_test[:, selected_feature_indices]\n \n# Cluster the training data (in input-output space) using FCM with default settings\ncl = Clusterer(x_train=x_train, y_train=y_train, nr_clus=nr_clus)\ncluster_centers, partition_matrix, _ = cl.cluster(method=\"fcm\")\n \n# Estimate the membership funtions of the system (default: mf_shape = gaussian)\nae = AntecedentEstimator(x_train=x_train, partition_matrix=partition_matrix)\nantecedent_parameters = ae.determineMF()\n\n# Calculate the firing strength of each rule for each data instance \nfsc=FireStrengthCalculator(antecedent_parameters=antecedent_parameters, nr_clus=nr_clus, variable_names=variable_names)\nfiring_strengths = fsc.calculate_fire_strength(data=x_train)\n\n# Estimate the parameters of the consequent functions\nce = ConsequentEstimator(x_train=x_train, y_train=y_train, firing_strengths=firing_strengths)\nconsequent_parameters = ce.suglms()\n \n# Build a first-order Takagi-Sugeno model using Simpful. Specify the optional \n# 'extreme_values' argument to specify the universe of discourse of the input\n# variables if you which to use Simpful's membership function plot functionalities.\nsimpbuilder = SugenoFISBuilder(antecedent_sets=antecedent_parameters, consequent_parameters=consequent_parameters, variable_names=variable_names)\nmodel = simpbuilder.get_model()\n\n# Calculate the mean squared error (MSE) of the model using the test data set\ntest=SugenoFISTester(model=model, test_data=x_test, variable_names=variable_names, golden_standard=y_test)\nMSE = test.calculate_MSE()\n\nprint('The mean squared error of the created model is', MSE)\n```\n\n### Example 4\n\n```\nfrom pyfume import pyFUME\nimport pandas as pd\nimport numpy as np\n\n# Read a Pandas dataframe (using the Pandas library)\ndf = pd.read_csv('.\\Concrete_data.csv')\n\n# Generate the Takagi-Sugeno FIS\nFIS = pyFUME(dataframe=df, nr_clus=2)\n\n# Calculate and print the accuracy of the generated model\nMAE=FIS.calculate_error(method=\"MAE\")\nprint (\"The estimated error of the developed model is:\", MAE)\n\n### Use the FIS to predict the compressive strength of a new concrete samples\n\n## Using Simpful's syntax (NOTE: This approach ONLY works for models built using non-normalized data!) \n# Extract the model from the FIS object\nmodel=FIS.get_model()\n\n# Set the values for each variable\nmodel.set_variable('Cement', 300.0)\nmodel.set_variable('BlastFurnaceSlag', 50.0)\nmodel.set_variable('FlyAsh', 0.0)\nmodel.set_variable('Water', 175.0)\nmodel.set_variable('Superplasticizer',0.7)\nmodel.set_variable('CoarseAggregate', 900.0)\nmodel.set_variable('FineAggregate', 600.0)\nmodel.set_variable('Age', 45.0)\n\n# Perform inference and print predicted value\nprint('The output using Simpfuls \"set_variable\" functionality is:', model.Sugeno_inference(['OUTPUT']))\n\n## Using pyFUME's syntax (NOTE: This approach DOES work for models built using normalized data!)\n# Create numpy array (matrix) in which each row is a data instance to be processed\nnew_data_one_instance=np.array([[300, 50,0,175,0.7,900,600,45]]) \nprediction_labels_one_instance=FIS.predict_label(new_data_one_instance)\nprint('The output using pyFUMEs \"predict_label\" functionality is:', prediction_labels_one_instance)\n\n# Example in which output for multiple data instances is computed\nnew_data_multiple_instances=np.array([[300, 50,0,175,0.7,900,600,45],[500, 75,30,200,0.9,600,760,39],[250, 40,10,175,0.3,840,360,51]]) \nprediction_labels_multiple_instance=FIS.predict_label(new_data_multiple_instances)\nprint('The output using pyFUMEs \"predict_label\" functionality is:', prediction_labels_multiple_instance)\n\n### Plot the actual values vs the predicted values of the test data using the matplotlib library\n\n# Predict the labels of the test data\npred = FIS.predict_test_data()\n\n# Get the actual labels of the test data\n_, actual = FIS.get_data(data_set='test')\n\n# Create scatterplot\nimport matplotlib.pyplot as plt \nplt.scatter(actual, pred)\nplt.xlabel('Actual value') \nplt.ylabel('Predicted value')\nplt.plot([0,85],[0,85],'r') # Add a reference line\nplt.show()\n\n\n```\n\n## Installation\n\n`pip install pyfume`\n\n\n## Further information\nIf you need further information, please write an e-mail to Caro Fuchs: c.e.m.fuchs(at)tue.nl.\n\n\n## References\n[1] Fuchs, C., Spolaor, S., Nobile, M. S., & Kaymak, U. (2020) \"pyFUME: a Python package for fuzzy model estimation\". In 2020 IEEE International Conference on Fuzzy Systems (FUZZ-IEEE) (pp. 1-8). IEEE.\n\n[2] I-Cheng Yeh, \"Modeling of strength of high performance concrete using artificial neural networks,\" Cement and Concrete Research, Vol. 28, No. 12, pp. 1797-1808 (1998). http://archive.ics.uci.edu/ml/datasets/Concrete+Compressive+Strength\n\n\n",
"bugtrack_url": null,
"license": "LICENSE.txt",
"summary": "A Python package for fuzzy model estimation",
"version": "0.3.4",
"project_urls": {
"Homepage": "https://github.com/CaroFuchs/pyFUME"
},
"split_keywords": [
"fuzzy logic",
" fuzzy inference systems",
" fuzzy model",
" data-driven",
" model estimation",
" machine learning"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "edeaa3b120e251145dcdb10777f2bc5f18b1496fd999d705a178c1b0ad947ce1",
"md5": "442a023fd2459124f7bcbc0bbc4e725a",
"sha256": "662724859befa5bf3b70f35cb3616b9f0b6c9e612e9a79edb8c169a03fc71b1f"
},
"downloads": -1,
"filename": "pyFUME-0.3.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "442a023fd2459124f7bcbc0bbc4e725a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 60304,
"upload_time": "2024-05-03T11:41:56",
"upload_time_iso_8601": "2024-05-03T11:41:56.989508Z",
"url": "https://files.pythonhosted.org/packages/ed/ea/a3b120e251145dcdb10777f2bc5f18b1496fd999d705a178c1b0ad947ce1/pyFUME-0.3.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "43553ca6936da251cb56e0a1de1249c25cde6bf89b2d36ceab7cfe12fe4053ad",
"md5": "50f0ae492ea7a127ba56fc22910d3544",
"sha256": "5305b93b015fbb4d650cac33ef6881d9e81b3a8c5bfadf149c41485236667df5"
},
"downloads": -1,
"filename": "pyfume-0.3.4.tar.gz",
"has_sig": false,
"md5_digest": "50f0ae492ea7a127ba56fc22910d3544",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 54104,
"upload_time": "2024-05-03T11:41:59",
"upload_time_iso_8601": "2024-05-03T11:41:59.090964Z",
"url": "https://files.pythonhosted.org/packages/43/55/3ca6936da251cb56e0a1de1249c25cde6bf89b2d36ceab7cfe12fe4053ad/pyfume-0.3.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-03 11:41:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "CaroFuchs",
"github_project": "pyFUME",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pyfume"
}