-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 06d797f
Showing
8 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
environment | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright (c) 2012 Joël Cox | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Miner | ||
===== | ||
|
||
Miner is a toy library for data mining. The main goal of this library is to provide an introduction to different data mining techniques while learning on the subject myself. This library isn't optimized for performance nor production use, but this might change at a later date. | ||
|
||
Quick start | ||
----------- | ||
|
||
A simple yet powerful algorithm for cluster analysis is the *k-means* algorithm. This algorithm will partition a set of objects over *k* clusters. You can run this algorithm using the code below. After the algorithm has converged, the `clusters` property of the `kmeans` objects (`kmeans.clusters`) will contain a dictionary with indexes that refer to the elements in `space.point`. | ||
|
||
import miner.utils | ||
import miner.clustering | ||
|
||
space = miner.utils.Space() | ||
space.point([(2, 2), (2, 1), (2, 3), (2, -2), (2, -1), (2, -3)]) | ||
|
||
kmeans = miner.clustering.KMeans(2, space) | ||
kmeans.converge() | ||
|
||
License | ||
------- | ||
This library is released under the MIT license. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import random | ||
|
||
from miner.utils import distance as dist | ||
|
||
class KMeans(object): | ||
|
||
def __init__(self, k, space): | ||
self.k = k | ||
self.space = space | ||
self.clusters = [] | ||
self.iteration = 0 | ||
|
||
# Set up our tree | ||
for k in range(self.k): | ||
self.clusters.append({'points': []}) | ||
|
||
def converge(self, **kwargs): | ||
"""Runs the algorithm for as much iterations | ||
to make the clusters converge | ||
""" | ||
|
||
self.random_centroids() | ||
while True: | ||
self.compute_distances() | ||
self.assign_points() | ||
if self.compute_centroids() == False: | ||
break | ||
|
||
kwargs['render'](self.space, self.clusters) | ||
|
||
def random_centroids(self): | ||
"""Selecter random centroids by generating random numbers, | ||
and adding the index to centroids list | ||
""" | ||
|
||
amount = len(self.space.points) - 1; | ||
self.centroids = [] | ||
|
||
for k in range(self.k): | ||
centroid = self.space.points[(random.randint(0, amount))] | ||
self.centroids.append(centroid) | ||
amount -= 1 | ||
|
||
def compute_distances(self): | ||
self.distances = [] | ||
|
||
for point_index in range(len(self.space.points)): | ||
distance = [] | ||
point = self.space.points[point_index] | ||
|
||
for centroid in self.centroids: | ||
distance.append(dist(centroid, point)) | ||
|
||
self.distances.append(distance) | ||
|
||
def assign_points(self): | ||
"""Loops over all points and assigns it to the correct cluster""" | ||
|
||
# Reset clusters | ||
self.clusters = [] | ||
for k in range(self.k): | ||
self.clusters.append({'points': []}) | ||
|
||
for point in range(len(self.space.points)): | ||
current_distances = self.distances.pop(0) | ||
lowest_distance = min(current_distances) | ||
cluster_index = current_distances.index(lowest_distance) | ||
self.clusters[cluster_index]['points'].append(point) | ||
|
||
def compute_centroids(self): | ||
|
||
centroids = [] | ||
centroids.extend(self.centroids) | ||
self.previous_centroids = centroids | ||
self.iteration = self.iteration + 1 | ||
|
||
# Compute the averages | ||
for cluster in range(len(self.clusters)): | ||
x = 0 | ||
y = 0 | ||
|
||
for point in self.clusters[cluster]['points']: | ||
x += self.space.points[point][0] | ||
y += self.space.points[point][1] | ||
|
||
# Make sure we don't device by zero | ||
length = len(self.clusters[cluster]['points']) | ||
if length != 0: | ||
self.centroids[cluster] = (x/length, y/length) | ||
else: | ||
self.centroids[cluster] = (x, y) | ||
|
||
# Do the centroids match? | ||
if self.centroids == self.previous_centroids: | ||
return False | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import math | ||
from types import ListType | ||
|
||
class Space(object): | ||
def __init__(self, dimension=2): | ||
if dimension < 2: | ||
raise ValueError('Dimension can\'t be smaller than 2') | ||
|
||
self.dimension = dimension | ||
self.points = [] | ||
|
||
def point(self, *args): | ||
"""Appends a point to the point lists and verifies | ||
its dimension. | ||
""" | ||
if (type(args[0]) is ListType): | ||
for tup in args[0]: | ||
self.point(*tup) | ||
return | ||
|
||
if len(args) != self.dimension: | ||
raise IndexError('Amount of arguments (%s) does not match space \ | ||
dimension (%s)' % (len(args), self.dimension)) | ||
|
||
floats = map(float, args) | ||
self.points.append(tuple(floats)) | ||
|
||
def distance(p, q): | ||
""" | ||
Compute the Euclidian distance between two points | ||
""" | ||
if len(p) != len(q): | ||
raise IndexError('The dimension of the two points don\'t match') | ||
|
||
total = 0 | ||
for i in range(len(p)): | ||
total += (p[i] - q[i])**2 | ||
|
||
return math.sqrt(total) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import sys | ||
sys.path.insert(0, os.path.abspath('..')) | ||
|
||
import unittest | ||
|
||
import miner.utils | ||
import miner.clustering | ||
|
||
|
||
class TestSpace(unittest.TestCase): | ||
|
||
def setUp(self): | ||
space = miner.utils.Space() | ||
space.point([(2, 2), (2, 1), (2, 3), (2, -2), (2, -1), (2, -3)]) | ||
|
||
self.kmeans = miner.clustering.KMeans(2, space) | ||
|
||
def test_amount_centroids(self): | ||
self.kmeans.random_centroids() | ||
self.assertEquals(len(self.kmeans.centroids), 2) | ||
self.assertTrue(map(lambda x: x < len(self.kmeans.centroids), | ||
self.kmeans.centroids)) | ||
|
||
def test_assign_points(self): | ||
# Manually set our centroids, instead of random, so | ||
# we can predict the output | ||
self.kmeans.centroids = [(2, 2), (2, -2)] | ||
|
||
self.kmeans.compute_distances(); | ||
self.kmeans.assign_points() | ||
|
||
self.assertEquals(self.kmeans.clusters, | ||
[ {'points': [0, 1, 2]}, | ||
{'points': [3, 4, 5]}]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
import sys | ||
sys.path.insert(0, os.path.abspath('..')) | ||
|
||
import unittest | ||
|
||
import miner.utils | ||
|
||
|
||
class TestSpace(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.space = miner.utils.Space(3) | ||
|
||
def test_float_conversion(self): | ||
self.space.point(1, 2, 3) | ||
self.assertEqual(self.space.points, [(1.00, 2.00, 3.00)]) | ||
|
||
def test_tuple_point(self): | ||
self.space.point([(1, 2, 3), (3, 2, 1)]) | ||
self.assertEqual(len(self.space.points), 2) | ||
|
||
def test_dimension_mismatch(self): | ||
self.assertRaises(IndexError, self.space.point, (1.00, 2.00)) | ||
|
||
def test_distance(self): | ||
# Wolfram `EuclidianDistance({1,2,3},{3,2,1}` | ||
self.assertEquals(miner.utils.distance((1, 2, 3), (3, 2, 1)), | ||
2.8284271247461903) |