-
Notifications
You must be signed in to change notification settings - Fork 0
/
23_5.py
32 lines (30 loc) · 1 KB
/
23_5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def genDistribution(xMean, xSD, yMean, ySD, n, namePrefix):
samples = []
for s in range(n):
x = random.gauss(xMean, xSD)
y = random.gauss(yMean, ySD)
samples.append(Example(namePrefix+str(s), [x,y]))
return samples
def plotSamples(samples, marker):
xVals, yVals = [], []
for s in samples:
x = s.getFeatures()[0]
y = s.getFeatures()[1]
pylab.annotate(s.getName(), xy = (x,y), mytext=(x+0.13,y-0.07), fontsize='x-large')
xVals.append(x)
yVals.append(y)
pylab.plot(xVals, yVals, marker)
def contrivedTest(numTrials, k, verbose = False):
xMean = 3
xSD = 1
yMean = 5
ySD = 1
n = 10
d1Samples = genDistribution(xMean, xSD, yMean, ySD, n, 'A')
plotSamples(d1Samples, 'k^')
d2Samples = genDistribution(xMean+3, xSD, yMean+1, ySD, n, 'B')
plotSamples(d2Samples, 'ko')
clusters = trymeans(d1Samples+d2Samples, k, numTrials, verbose)
print('Final Result')
for c in clusters:
print(" ", c)