Python

setup easy_install ubuntu:

sudo aptitude install python-setuptools

numpy package notes

import numpy as np
column1, var2, var3 = np.loadtxt(filename, unpack="True")
#Print arrays to a file
file_handle = file('name.ascii','a')
np.savetxt(file_handle, 1_D_array_name )
file_handle.close()

So many little tips and tricks…

Say you had an array where you wanted to take some smaller section of the array and do something with it — average it, let's say — and then move on to the next section and do that again through the end of your array.

import numpy as np
x = range(10)
[np.average(x[i:i+4]) for i in xrange(0, len(x), 4) ]
[1.5, 5.5, 8.5]

If you had a really long array, and wanted to know what the standard deviation of the averages of the arrays as you took larger and larger bites, you could do something like this:

for j in range(50,10000,50):
    print j, np.std([np.average(large_array[i:i+j]) for i in xrange(0,len(large_array),j)] )

Randomly select from a list:

import random
random.choice(monte_expo_2[5][0])
-2.0029793840199839e-06

Randomly choose a set (w/o replacement) from a list (if you want w/ replacement, just do the random.choice the number of times you want):

import random
random.sample( monte_expo_2[5][0], 3 )
[-1.422382476303873e-06, -1.1211891540471395e-06, 3.2895286949584497e-06]
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.