-3
for j in range(0, NumberOfFeatures):
    for k in range(j+1,NumberOfFeatures):
        countArray = np.ones((2,2))
        for i in range(0,NumberOfTrainingExamples):
            countArray[XTrain[i,j],XTrain[i,k]] += 1

The innermost for loop takes quite some time for large NumberOfFeatures, NumberOfTrainingExamples

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46
  • this code sample is incomplete – touch my body Apr 20 '18 at 13:26
  • I need the countArray for every j&k. I use it to compute few other parameters and then it can be reset. Which is why I need faster computing only for the innermost for-loop. – Mythri Thippareddy Apr 20 '18 at 13:30
  • this code sample is incomplete – touch my body Apr 20 '18 at 13:31
  • 2
    Since you never refer to `countArray` after the loop ends, and indeed the code produces no output at all, you can delete the loops entirely and the outcome will be the same, but much faster. (If you're thinking "but I _do_ use `countArray` after the loop, and I do output information", that code should also be in the question as part of your [mcve]) – Kevin Apr 20 '18 at 13:33
  • I don't think you can make that run any faster. Arrays in Python are slow. If you need fast arrays consider PHP. Otherwise try approach your problem in a different way – put it in a database and write a query (for example). https://stackoverflow.com/questions/36778568/why-are-pythons-arrays-slow – geoidesic Apr 20 '18 at 13:35

1 Answers1

0

Its O(n^3) basically (where n is not the same number).

Because the code is not complete it is very hard to determine what could be done better but from what you provided, try to reduce it to at least n^2 otherwise it will just take some time.

If you have 10 of each its 1000 cycles, 1000 of each is 1,000,000,000 so with bigger numbers it gets hard to calculate very fast.

eXPRESS
  • 425
  • 2
  • 4
  • 19