-2

I have this chunk of code that predicts on a model and I feel that there is a way to write a for loop to shorten it. However, I am not too sure how to do so as I would have to create variables within the for loop. Would it be possible to shorten this code to a for loop:

    #remainder1 through model

    _remainder1FV, _remainder1Out, _remainder1OutID = _detector.get_fv(_deFV, _deMeta['_remainID1'])
    _remainder1PredOut = _detector.classifer_prediction(_remainder1FV, _deMeta)
    _remainder1Result = _detector.result_calculation(_remainder1Out, _remainder1PredOut, _deMeta, tag='_remainID1')


    #remainder2 through model

    _remainder2FV, _remainder2Out, _remainder2OutID = _detector.get_fv(_deFV, _deMeta['_remainID2'])
    _remainder2PredOut = _detector.classifer_prediction(_remainder2FV, _deMeta)
    _remainder2Result = _detector.result_calculation(_remainder2Out, _remainder2PredOut, _deMeta, tag='_remainID2')


    #remainder3 through model

    _remainder3FV, _remainder3Out, _remainder3OutID = _detector.get_fv(_deFV, _deMeta['_remainID3'])
    _remainder3PredOut = _detector.classifer_prediction(_remainder3FV, _deMeta)
    _remainder3Result = _detector.result_calculation(_remainder3Out, _remainder3PredOut, _deMeta, tag='_remainID3')


    #remainder4 through model

    _remainder4FV, _remainder4Out, _remainder4OutID = _detector.get_fv(_deFV, _deMeta['_remainID4'])
    _remainder4PredOut = _detector.classifer_prediction(_remainder4FV, _deMeta)
    _remainder4Result = _detector.result_calculation(_remainder4Out, _remainder4PredOut, _deMeta, tag='_remainID4')
Ruven Guna
  • 414
  • 7
  • 25
  • 1
    I do not think that codereview will welcome this. – Patrick Artner Sep 14 '18 at 08:46
  • 3
    Having a bunch of numbered variables like that is not a good idea. You should store each group of remainders in a custom class, or maybe a dict or namedtuple, and then put the groups into a list. – PM 2Ring Sep 14 '18 at 08:47
  • I would probably put this into some kind of method - but you are passing a lot of "not show" stuff in - but something akin to : `def doIt(detector,FV,Meta,tag): ... return a,b,c` and then simply stuff that tuple into a list while you iterate over `for t in ['_remainID1','_remainID2,'_remainID3','_remainID4'] : someList.append(doIt(...))` – Patrick Artner Sep 14 '18 at 08:48
  • 1
    @PatrickArtner Sure a plain "anonymous" tuple would work, but I think it's nicer if we can retain the names. – PM 2Ring Sep 14 '18 at 08:50
  • Do you want to keep all 5 remainders in each group? – PM 2Ring Sep 14 '18 at 08:58

2 Answers2

1

Why don't you just use a dictionary to save your results?

results={};
for id in range(1,5):
    _remainderFV, _remainderOut, _remainderOutID = _detector.get_fv(_deFV, _deMeta['_remainID'+str(id)])
    _remainderPredOut = _detector.classifer_prediction(_remainderFV, _deMeta)
    _remainderResult = _detector.result_calculation(_remainderOut, _remainderPredOut, _deMeta, tag='_remainID'+str(id))
    results[id]=_remainderResult
msi_gerva
  • 2,021
  • 3
  • 22
  • 28
  • I suspect that the OP wants to retain all 5 remainders in each group, or at least more than just the `remainderResult`. – PM 2Ring Sep 14 '18 at 08:54
  • He could use a list like this: `results[id]=[_remainderFV, _remainderOut, _remainderOutID,_remainderPredOut,_remainderResult]`. Nevertheless, the repetion of the code is gone. – msi_gerva Sep 14 '18 at 08:57
-1

Yes, I think this should work for you :)

_remainderResults = []
for i in ('_remainID1', '_remainID2', '_remainID3', '_remainID4'):
    _remainderFV, _remainderOut, _remainderOutID = _detector.get_fv(_deFV, _deMeta[i])
    _remainderPredOut = _detector.classifer_prediction(_remainderFV, _deMeta)
    _remainderResults.append(_detector.result_calculation(_remainderOut, _remainderPredOut, _deMeta, tag=i))

_remainder1Result, _remainder2Result, _remainder3Result, _remainder4Result = _remainderResults
simopopov
  • 854
  • 5
  • 12
  • 2
    You hardcode the variables: '_remainID1' to '_remainID4'... What happens, if someone has 1000 variables? – msi_gerva Sep 14 '18 at 08:59
  • omg ... I will use for i in range(100) and then '_remainID{}'.format(i) .... the guy asks about this example not in general. In this case I will do it like in my example. If the case is with 100 variables, I will use the other approach. Anyway ... click Down vote and be happy ;) – simopopov Sep 14 '18 at 09:09