Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
2012 views
Kernel: Python 3 (Anaconda)
# !pip install -U scikit-fuzzy # http://pythonhosted.org/scikit-fuzzy/auto_examples/index.html # Instalar via terminal
import numpy as np import skfuzzy as fuzz from skfuzzy import control as ctrl import matplotlib.pyplot as plt
t = ctrl.Antecedent(np.arange(0, 50, 1), 'T') t['baixo'] = fuzz.sigmf(t.universe, 15, -1) t['medio'] = fuzz.gaussmf(t.universe, 25, 3) t['alto'] = fuzz.pimf(t.universe, 30, 45, 50, 75) t.view()
/ext/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, "
Image in a Jupyter notebook
fur = ctrl.Antecedent(np.arange(0, 50, 1), 'furtos') fur['baixo'] = fuzz.trimf(fur.universe, [0, 0, 20]) fur['medio'] = fuzz.gbellmf(fur.universe, 8, 6, 25) fur['alto'] = fuzz.pimf(fur.universe, 30, 45, 50, 75) fur.view()
/ext/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, "
Image in a Jupyter notebook
rou = ctrl.Antecedent(np.arange(0, 50, 1), 'roubos') rou['baixo'] = fuzz.trimf(rou.universe, [0, 0, 23]) rou['medio'] = fuzz.gbellmf(rou.universe, 7, 7, 25) rou['alto'] = fuzz.pimf(rou.universe, 30, 40, 45, 70) rou.view()
/ext/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, "
Image in a Jupyter notebook
hom = ctrl.Antecedent(np.arange(0, 10.1, 0.1), 'homicidios') hom['baixo'] = fuzz.sigmf(hom.universe, 2, -7) hom['medio'] = fuzz.trapmf(hom.universe, [2, 4, 6, 8]) hom['alto'] = fuzz.trimf(hom.universe, [7, 10, 10]) hom.view()
/ext/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, "
Image in a Jupyter notebook
crim = ctrl.Consequent(np.arange(0, 101, 1), 'criminalidade') crim['baixo'] = fuzz.zmf(crim.universe, 0, 40) crim['medio'] = fuzz.gaussmf(crim.universe, 50, 10) crim['alto'] = fuzz.trimf(crim.universe, [65, 100, 100]) crim.view()
/ext/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, "
Image in a Jupyter notebook
fur['baixo'].view()
/ext/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, "
Image in a Jupyter notebook
regra1 = ctrl.Rule(rou['baixo'] & fur['baixo'] & hom['baixo'], crim['baixo']) regra2 = ctrl.Rule((rou['medio'] | fur['medio']) & hom['baixo'], crim['medio']) regra3 = ctrl.Rule((rou['alto'] | fur['alto']) | (hom['alto'] | hom['medio']), crim['alto']) regra4 = ctrl.Rule(t['alto'] | t['medio'], crim['medio'])
# Cria um sistema de controle e uma simulação crim_ctrl = ctrl.ControlSystem([regra1, regra2, regra3, regra4]) crim_sim = ctrl.ControlSystemSimulation(crim_ctrl)
# Entrada crim_sim.input['roubos'] = 10 crim_sim.input['furtos'] = 15 crim_sim.input['homicidios'] = 2 crim_sim.input['T'] = 20 # Calcula crim_sim.compute() crim_sim.output
OrderedDict([('criminalidade', 36.003877274233218)])
crim.view(sim=crim_sim)
/ext/anaconda3/lib/python3.5/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure "matplotlib is currently using a non-GUI backend, "
Image in a Jupyter notebook
def ic(crim): # Entrada crim_sim.input['homicidios'] = crim[0] crim_sim.input['roubos'] = crim[1] crim_sim.input['furtos'] = crim[2] crim_sim.input['T'] = crim[3] # Calcula crim_sim.compute() return(crim_sim.output['criminalidade'])
ic([10, 15, 2, 20])
73.847733658811876
import pandas as pd
df = pd.read_excel('Crimes e temperaturas.xlsx', sheet_name='CUIABA') df.set_index(['anp', 'mes'])
df.loc[0,"T"]
27.806451612903224
aux = df[["homicidio", "roubo", "furto", "T"]]
aux["IC"] = aux.apply(ic, axis = 1)
/ext/anaconda3/lib/python3.5/site-packages/ipykernel/__main__.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy if __name__ == '__main__':
aux