lundi 1 août 2016

Student: How to isolate and tally hit frequency within a np.random.choice range

Currently learning Python and very new to Numpy & Panda

I have pieced together a random generator with a range. It uses Numpy and I am unable to isolate each individual result to count the iterations within a range.

Goal: Count the iterations of "Random >= 1000" and then add 1 to the appropriate column that correlates to the tally of iterations. Example in very basic sense:

#Random generator begins... these are first four random generations
Randomiteration1 = 175994 (Random >= 1000)
Randomiteration2 = 1199 (Random >= 1000)
Randomiteration3 = 873399 (Random >= 1000)
Randomiteration4 = 322 (Random < 1000)

#used to +1 to the fourth row of column A in CSV
finalIterationTally = 4

#total times random < 1000 throughout entire session. Placed in cell B1
hits = 1

(The logic would then be to +1 to A4 in the spreadsheet. If the iteration tally would have been 7, then +1 to the 7th column, etc. So essentially, I am measuring the distance and frequency of that distance between each "Hit")

My current code includes a CSV export portion. I do not need to export each individual random result any longer. I only need to export the frequency of each iteration distance between each hit. This is where I am stumped.

Cheers

import pandas as pd
import numpy as np

#set random generation quantity
generations=int(input("How many generations?\n###:"))

#random range and generator
choices = range(1, 100000)
samples = np.random.choice(choices, size=generations)

#create new column in excel
my_break = 1000000
if generations > my_break:
    n_empty = my_break - generations % my_break
    samples = np.append(samples, [np.nan] * n_empty).reshape((-1, my_break)).T

#export results to CSV
(pd.DataFrame(samples)
 .to_csv('eval_test.csv', index=False, header=False))

#left uncommented if wanting to test 10 generations or so
print (samples)




Aucun commentaire:

Enregistrer un commentaire