python - Efficient way to loop through large numpy arrays -


i'm working on building fuzzy inference system skfuzzy , need find way speed code:

import skfuzzy fuzz skfuzzy import control ctrl import numpy np   def fis(s, r):     #generate universe variables     = ctrl.antecedent(np.arange(0, 70.1, 0.1), 'a')     b = ctrl.antecedent(np.arange(0, 6.01, 0.01), 'b')     c = ctrl.consequent(np.arange(0, 12.01, 0.01), 'c')      #generate fuzzy membership functions     #a     a['l'] = fuzz.trapmf(a.universe, [0.0, 0.0, 3.0, 6.0])     a['m'] = fuzz.trapmf(a.universe,[3.0, 6.0, 16.0, 24.0])     a['h'] = fuzz.trapmf(a.universe, [16.0, 24.0, 30.0, 45.0])     a['e'] = fuzz.trapmf(a.universe, [30.0, 45.0, 70.0, 70.0])      #b     b['l'] = fuzz.trapmf(b.universe, [0, 0, 0.01, 0.02])     b['m'] = fuzz.trapmf(b.universe,[0.01, 0.02, 0.03, 0.05])     b['h'] = fuzz.trapmf(b.universe, [0.03, 0.05, 0.10, 0.12])     b['e'] = fuzz.trapmf(b.universe, [0.10, 0.12, 6.00, 6.00])      #c     c['l'] = fuzz.trapmf(c.universe, [0, 0, 0.01, 0.02])     c['m'] = fuzz.trapmf(c.universe,[0.01, 0.02, 0.04, 0.05])     c['h'] = fuzz.trapmf(c.universe, [0.04, 0.05, 0.10, 0.20])     c['e'] = fuzz.trapmf(c.universe, [0.10, 0.20, 12.00, 12.00])      #fuzzy rules     rule1 = ctrl.rule(a['l'] & b['l'], c['l'])     rule2 = ctrl.rule(a['l'] & b['m'], c['m'])     rule3 = ctrl.rule(a['l'] & b['h'], c['h'])     rule4 = ctrl.rule(a['m'] & b['l'], c['m'])     rule5 = ctrl.rule(a['m'] & b['m'], c['m'])     rule6 = ctrl.rule(a['m'] & b['h'], c['h'])     rule7 = ctrl.rule(a['h'] & b['l'], c['h'])     rule8 = ctrl.rule(a['h'] & b['m'], c['h'])     rule9 = ctrl.rule(a['h'] & b['h'], c['e'])     rule10 = ctrl.rule(a['e'], c['e'])     rule11 = ctrl.rule(b['e'], c['e'])      #control system     c_ctrl = ctrl.controlsystem([rule1, rule2, rule3, rule4, rule5, rule6,     rule7, rule8, rule9, rule10, rule11])     c_simulation = ctrl.controlsystemsimulation(c_ctrl)      c_simulation.input['a'] = s     c_simulation.input['b'] = r      c_simulation.compute()      value = c_simulation.output['c']      return value  #fake data s_data = np.random.randomstate(1234567890) s_data = s_data.randint(0, 70, size=600000)  r_data = np.random.random_sample(600000)  vec1 = s_data.flatten().astype('float') vec2 = r_data.flatten().astype('float')  #pre allocate output array cert = np.zeros(np.shape(vec1))*np.nan  #find index of finite elements of array ind = np.where(np.isfinite(vec1))[0]  # classify  k in xrange(len(ind)):    cert[ind[k]] = fis(vec1[ind[k]], vec2[ind[k]]) 

my calculations taking more 10+ hours complete. how perform these calculations without loop? ideally, looking numpy solution, open alternative solutions.

i'd give credit jdwarner scikit-fuzzy google group excellent answer above question. think other scikit-fuzzy users find posting useful in future.

https://groups.google.com/forum/#!topic/scikit-fuzzy/aoo0inuheuo


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -