Sunday, February 28, 2010

[Py] Random data generation -- a trial

I am studying the usage of LIBSVM recently. What I want is to test the library with some simple data to enhance my understanding for using the library. Therefore, I thought about generating some random data sets for my own learning purpose, and of course, the first choice came into me was Python.

The following are my code and the resulting data plot.
---------------------------------------------
Code:
from pylab import *
from numpy import *

data =[]
#generate random data sets
data.append( 1.2*random.randn(2,30) )
data.append( 2.0*random.randn(2,30) )
data.append( random.randn(2,30) )

#shift the data sets
x0 = data[0][0]; y0 = data[0][1]
x1 = data[1][0]+3.5; y1 = data[1][1]+2.5
x2 = data[2][0]-1.5; y2 = data[2][1]+4.5

f = open('randData','w')
for i in range(len(x0)):
print >> f, '%f %f %f %f %f %f'\
% (x0[i], y0[i], x1[i], y1[i], x2[i], y2[i])

f.close()

plot(x0, y0, 'ro', x1, y1, 'go', x2, y2, 'bo')
savefig('randData.png')
show()


No comments:

Post a Comment