-
Notifications
You must be signed in to change notification settings - Fork 149
/
test_prob.py
executable file
·52 lines (29 loc) · 1.67 KB
/
test_prob.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env python
# Copyright 2013 Tom SF Haines
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
import random
import numpy
from ms import MeanShift
# Create a dataset - equally spaced samples weighted by a Gaussian, such that it should estimate a Gaussian...
x = numpy.arange(-5.0, 5.0, 0.02)
y = numpy.exp(-0.5 * x**2.0 / 2.0)
data = numpy.concatenate((x.reshape((-1,1)), y.reshape((-1,1))), axis=1)
# Setup the mean shift object...
ms = MeanShift()
ms.set_data(data, 'df')
normal_kernels = ['uniform', 'triangular', 'epanechnikov', 'cosine', 'gaussian', 'cauchy', 'logistic']
ms.set_kernel(random.choice(normal_kernels))
ms.set_spatial(random.choice(ms.spatials()))
ms.set_scale(numpy.ones(2), 1)
# Iterate and calculate the probability at every point...
sam = numpy.arange(-5.0, 5.0, 0.15)
prob = numpy.array(map(lambda v: ms.prob(numpy.array([v,1.0])), sam))
# Print out basic stats...
print 'kernel = %s; spatial = %s' % (ms.get_kernel(), ms.get_spatial())
print 'exemplars = %i; features = %i' % (ms.exemplars(), ms.features())
print
# Visualise the output...
for threshold in numpy.arange(prob.max(), 0.0, -prob.max()/15.0):
print ''.join(map(lambda p: '|' if p>threshold else ' ', prob))