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.

2
  • Its hard to understand how your input looks like but this might be helpful.docs.python.org/2/library/… Commented Oct 1, 2018 at 16:43
  • How is collections.Counter.most_common relevant here? Commented Oct 1, 2018 at 16:44

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

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

12 Comments

How can I add this into a for loop and get the values for i for the max values.
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.
@hluk123 to rephrase dose each iteration of if i <3 have a top ten
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
@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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.