0

I am trying to find the top 10/300 values in a for loop. I understand how to find the max value for the entire loop, but I am trying to calculate 300 values and then only use the top 10 values in those 300.

Here is my for loop:

random.shuffle(folder)
for i in range(len(folder)):
if i < 30:
    phaseresult_i = []
    data = np.loadtxt(dir + folder[i])
    time = data[:,0]-2450000
    magnitude = data[:,1]
    #print ('\n File:', folder[i],'\n','Time:',time,'\n', 'Magnitude:', magnitude)
    t = 10000 * time
    y = np.sin(2 * np.pi * t) * time
    frequency, power = LombScargle(t, y).autopower()
    period = np.log(1/frequency)[np.argmax(power)]
    maxpower = power.max()
    for t in range(len(time)):
        #print(t,time[t])
        floor = math.floor((time[t]-time[0])/period)
        phase_i = ((time[t]-time[0])/period)-floor
        phaseresult_i.append(phase_i)
    maxpower_i.append(maxpower)   
    folder_i.append(folder[i])
else: 
    break

The value I am trying to find the maximums are the top ten max powers and their Periods.

hlku2334
  • 63
  • 2
  • 8
  • Its hard to understand how your input looks like but this might be helpful.https://docs.python.org/2/library/collections.html#collections.Counter.most_common – mad_ Oct 01 '18 at 16:43
  • How is `collections.Counter.most_common` relevant here? – rahlf23 Oct 01 '18 at 16:44

2 Answers2

2

You can use heapq.nlargest with a generator to give the top 10 values of an iterable. Here's a trivial example:

from random import sample
from heapq import nlargest

def gen(n):
    yield from sample(range(n), k=n)

res = nlargest(10, gen(100))

[99, 98, 97, 96, 95, 94, 93, 92, 91, 90]

You now only need to convert your logic into a generator function (see 1, 2).

jpp
  • 159,742
  • 34
  • 281
  • 339
1

For demonstration purposes, I shuffled a list of 300 integers, to get the 10 highest values, you would use sorted and then take the range [-10:]

import random

l = [*range(300)]
random.shuffle(l)

print(sorted(l)[-10:])
[290, 291, 292, 293, 294, 295, 296, 297, 298, 299]

Update

random.shuffle(folder)
for i in range(len(folder)):
  max_powers = []
  if i < 3:
    max_powers = []
    phaseresult_i = []
    data = np.loadtxt(dir + folder[i])
    time = data[:,0]-2450000
    magnitude = data[:,1]
    print ('\n File:', folder[i],'\n','Time:',time,'\n', 'Magnitude:', magnitude)
    t = 10000 * time
    y = np.sin(2 * np.pi * t) * time
    # Lomb Scargle Periodogram
    frequency, power = LombScargle(t, y).autopower()
    for period in folder[i]:
        period = np.log(1/frequency)[np.argmax(power)]
        maxpower = power.max()
    max_powers.append((maxpower, i))
    print('\n Max Power = ', maxpower)
    print('\n Period = ', period, '\n ---------------------------')
  print('\n Max Powers, Folder = {}'.format(sorted(max_powers, key=lambda x: x[0])[:-10]))
else:
    break
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
  • How can I add this into a for loop and get the values for i for the max values. – hlku2334 Oct 02 '18 at 13:43
  • For the top ten max powers, I am trying to get the top ten overall from all folders(files) and their associated folder for each max power. – hlku2334 Oct 02 '18 at 19:03
  • @hluk123 to rephrase dose each iteration of `if i <3` have a `top ten` – vash_the_stampede Oct 02 '18 at 19:09
  • So if i <30 (its just <3 now so I didnt have to run so many files), each i will have a max power. And then once its done going through the 30 files, I want to see top 10 max powers and which files they come from – hlku2334 Oct 02 '18 at 19:19
  • I've updated the code in the question, hopefully its more clear – hlku2334 Oct 02 '18 at 19:22
  • @hluk123 hard to test since I'm just playing it out in my head, but try this now – vash_the_stampede Oct 02 '18 at 19:30
  • error message says .append() can only have one input – hlku2334 Oct 02 '18 at 19:39
  • This helps a lot, thank you! So, after making all the edits it prints out the max power for each file and the file it comes from. Now, It still doesn't choose the top ten powers from the list that it has printed out. I only would like those to print out. – hlku2334 Oct 02 '18 at 20:02
  • 1
    `print('\n Max Powers, Folder = {}'.format(sorted(max_powers, key=lambda x: x[0])[:-10]))` oops forgot this, add the slice in and try, edited this comment fixed parentheses – vash_the_stampede Oct 02 '18 at 20:03
  • @hluk123 ah ha! thats awesome I was just trying to imagine it all, worked out hehehe ;) cheers! – vash_the_stampede Oct 02 '18 at 20:21
  • One quick question (since you already know what's going on in my code), How would I be able to take the files printed out and use those in another for loop? – hlku2334 Oct 02 '18 at 20:25
  • 1
    @hluk123 you could just do `for i in sorted(max_powers, key=lambda x: x[0])[:-10]` if you just wanted the powers not the file as well you would use `i[0]` and the `i[1]` would represent the file if you wanted to isolate either. You could even make a new list , `new_list = [i for i in sorted(max_powers, key=lambda x: x[0])[:-10]]` again you could isolate using `i[0]` / `i[1]` if you dont need the entire tuple – vash_the_stampede Oct 02 '18 at 20:31