def compare(m, Nobs, Ncodes, Nfeatures): obs = RandomArray.normal(0., 1., (Nobs, Nfeatures)) codes = RandomArray.normal(0., 1., (Ncodes, Nfeatures)) import scipy.cluster.vq scipy.cluster.vq print( 'vq with %d observation, %d features and %d codes for %d iterations' % (Nobs, Nfeatures, Ncodes, m)) t1 = time.time() for i in range(m): code, dist = scipy.cluster.vq.py_vq(obs, codes) t2 = time.time() py = (t2 - t1) print(' speed in python:', (t2 - t1) / m) print(code[:2], dist[:2]) t1 = time.time() for i in range(m): code, dist = scipy.cluster.vq.vq(obs, codes) t2 = time.time() print(' speed in standard c:', (t2 - t1) / m) print(code[:2], dist[:2]) print(' speed up: %3.2f' % (py / (t2 - t1))) # load into cache b = vq(obs, codes) t1 = time.time() for i in range(m): code, dist = vq(obs, codes) t2 = time.time() print(' speed inline/blitz:', (t2 - t1) / m) print(code[:2], dist[:2]) print(' speed up: %3.2f' % (py / (t2 - t1))) # load into cache b = vq2(obs, codes) t1 = time.time() for i in range(m): code, dist = vq2(obs, codes) t2 = time.time() print(' speed inline/blitz2:', (t2 - t1) / m) print(code[:2], dist[:2]) print(' speed up: %3.2f' % (py / (t2 - t1))) # load into cache b = vq3(obs, codes) t1 = time.time() for i in range(m): code, dist = vq3(obs, codes) t2 = time.time() print(' speed using C arrays:', (t2 - t1) / m) print(code[:2], dist[:2]) print(' speed up: %3.2f' % (py / (t2 - t1)))
def compare(m, Nobs, Ncodes, Nfeatures): obs = RandomArray.normal(0., 1., (Nobs, Nfeatures)) codes = RandomArray.normal(0., 1., (Ncodes, Nfeatures)) import scipy.cluster.vq scipy.cluster.vq print 'vq with %d observation, %d features and %d codes for %d iterations' % \ (Nobs,Nfeatures,Ncodes,m) t1 = time.time() for i in range(m): code, dist = scipy.cluster.vq.py_vq(obs, codes) t2 = time.time() py = (t2 - t1) print ' speed in python:', (t2 - t1) / m print code[:2], dist[:2] t1 = time.time() for i in range(m): code, dist = scipy.cluster.vq.vq(obs, codes) t2 = time.time() print ' speed in standard c:', (t2 - t1) / m print code[:2], dist[:2] print ' speed up: %3.2f' % (py / (t2 - t1)) # load into cache b = vq(obs, codes) t1 = time.time() for i in range(m): code, dist = vq(obs, codes) t2 = time.time() print ' speed inline/blitz:', (t2 - t1) / m print code[:2], dist[:2] print ' speed up: %3.2f' % (py / (t2 - t1)) # load into cache b = vq2(obs, codes) t1 = time.time() for i in range(m): code, dist = vq2(obs, codes) t2 = time.time() print ' speed inline/blitz2:', (t2 - t1) / m print code[:2], dist[:2] print ' speed up: %3.2f' % (py / (t2 - t1)) # load into cache b = vq3(obs, codes) t1 = time.time() for i in range(m): code, dist = vq3(obs, codes) t2 = time.time() print ' speed using C arrays:', (t2 - t1) / m print code[:2], dist[:2] print ' speed up: %3.2f' % (py / (t2 - t1))
def ThermalizingTransformer(atoms, T): """ Thermalizes velocities to m v^2 / 2 = kB T/2, with kB = 1 """ # vRMS = numpy.sqrt(T / atoms.mass) atoms.velocities = RandomArray.normal(0, vRMS, numpy.shape(atoms.velocities))
def statistics(): pd = stats.norm(loc=1, scale=0.5) # normal distribution N(1,0.5) n=10000 r = pd.rvs(n) # random variates import RandomArray r = RandomArray.normal(1, 0.1, n) s = stats.stats print pd.stats() print 'mean=%g stdev=%g skewness=%g kurtosis=%g' % \ (s.mean(r), s.variation(r), s.skew(r), s.kurtosis(r)) bin_counts, bin_min, min_width, noutside = s.histogram(r, numbins=50)
def statistics(): pd = stats.norm(loc=1, scale=0.5) # normal distribution N(1,0.5) n = 10000 r = pd.rvs(n) # random variates import RandomArray r = RandomArray.normal(1, 0.1, n) s = stats.stats print pd.stats() print 'mean=%g stdev=%g skewness=%g kurtosis=%g' % \ (s.mean(r), s.variation(r), s.skew(r), s.kurtosis(r)) bin_counts, bin_min, min_width, noutside = s.histogram(r, numbins=50)
nbins = self.array.shape[0] histo = N.add.reduce(weights*N.equal(N.arange(nbins)[:,N.NewAxis], data), -1) histo[-1] = histo[-1] + N.add.reduce(N.repeat(weights, N.equal(nbins, data))) self.array[:, 1] = self.array[:, 1] + histo if __name__ == '__main__': if N.package == 'Numeric': import RandomArray as random elif N.package == 'NumPy': from numpy import random nsamples = 1000 random.seed(12,13) data = random.normal(1.0, 0.5, nsamples) h = Histogram(data, 50) # use 50 bins between min & max samples h.normalizeArea() # make probabilities in histogram x = h.getBinIndices() y = h.getBinCounts() # add many more samples: nsamples2 = nsamples*100 data = random.normal(1.0, 0.5, nsamples2) h.addData(data) h.normalizeArea() x2 = h.getBinIndices() y2 = h.getBinCounts() # and more: nsamples3 = nsamples*1000
from Numeric import dot,sum import sys,numeric_version import RandomArray import LinearAlgebra print sys.version print "Numeric version:",numeric_version.version RandomArray.seed(123,456) a = RandomArray.normal(0,1,(100,10)) f = RandomArray.normal(0,1,(10,30)) e = RandomArray.normal(0,0.1,(100,30)) print "Got to seed:",RandomArray.get_seed() b = dot(a,f)+e (x,res,rank,s)=LinearAlgebra.linear_least_squares(a,b) f_res = sum((b-dot(a,f))**2) x_res = sum((b-dot(a,x))**2) print "'Planted' residues, upper bound for optimal residues:" print f_res print "Claimed residues:" print res print "Actual residues:" print x_res print "Ratio between actual and claimed (shoudl be 1):" print x_res/res print "Ratio between actual and planted (should be <1):" print x_res/f_res
sampleRate = 44100. c = 300. # meters per second spectralRange = sampleRate / 2 import numpy import math import RandomArray import stats T=r/c spectrum = numpy.zeros( nBins-1, numpy.complex) for x in RandomArray.normal(0,standardMicrophoneDeviation,(nSpeakers,)) : t1root = 1+x t2root = 1-x print t1root, t2root spectrum += numpy.array([ complex(1, t1root*w*T) * math.e**complex(math.cos(w*T*t1root), math.sin(w*T*t1root)) /x / w / w / T / T - complex(1, t2root*w*T) * math.e**complex(math.cos(w*T*t2root), math.sin(w*T*t2root)) /x / w / w / T / T for w in [ math.pi*2*normfreq*spectralRange/nBins for normfreq in xrange(1,nBins) ] ]) import Gnuplot gp=Gnuplot.Gnuplot(persist=1) gp('set data style lines') gp.plot(abs(spectrum), [bin.real for bin in spectrum], [bin.imag for bin in spectrum], numpy.zeros(nBins)) gp.hardcopy(filename="IncoherenceSimulation.png",terminal="png")
import RandomArray import numpy from numpy.random import normal import sys import fcs import pylab sys.path.append('../') import flow if __name__ == '__main__': data = numpy.concatenate((RandomArray.normal(5, 1, (2000, 2)), RandomArray.normal(7, 1, (2000, 2)), RandomArray.normal(9, 1, (3000, 2)), RandomArray.normal(11, 1, (2000, 2)), RandomArray.normal(13, 1, (1000, 2))), axis=0) # f = fcs.FCSReader("../data/3FITC-4PE.004.fcs") # print f.data.keys() # m = 10000 # x1 = numpy.array((f.data['FSC-H'])[:m], 'd') # x2 = numpy.array((f.data['SSC-H'])[:m], 'd') # x3 = numpy.array((f.data['FL1-H'])[:m], 'd') # x4 = numpy.array((f.data['FL2-H'])[:m], 'd') # print min(x1), max(x1) # print min(x2), max(x2) # print min(x3), max(x3) # print min(x4), max(x4) # data_unscaled = numpy.transpose([x1, x2, x3, x4]) # #data = numpy.transpose([(x1-min(x1))/max(x1), (x2-min(x2))/max(x2), (x3-min(x3))/max(x3), (x4-min(x4))/max(x4)])
def NumPy_random(n): import RandomArray return RandomArray.normal(0,1,n)