• Stars
    star
    286
  • Rank 143,861 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple implementation of mean shift clustering in python

Mean Shift Clustering

MeanShift_py is a simple implementation of mean shift clustering in python.

Dependencies

The only dependency is Numpy

Description

The mean_shift.py module defines a class called MeanShift. The MeanShift class constructor takes in an optional kernel parameter. If no kernel is specified, a default Gaussian kernel is used.

The cluster method requires an array of points and a kernel bandwidth value. A optional iteration_callback function can also be passed in that will be called back at the end of each mean shift iteration with the current state of the algorithm (e.g., where the points are currently at, along with an iteration number).

After the clustering finishes, a MeanShiftResult object is returned, containing three arrays:

  1. The original points
  2. The shifted points
  3. Cluster assignments for each point

Usage

import mean_shift as ms

data = get_data_from_somewhere()
mean_shifter = ms.MeanShift()
mean_shift_result = mean_shifter.cluster(data, kernel_bandwidth = 10)

original_points =  mean_shift_result.original_points
shifted_points = mean_shift_result.shifted_points
cluster_assignments = mean_shift_result.cluster_ids

# If you want to use multivariate gaussian kernel
# By default it uses unviariate gaussian kernel
# Make sure the dimensions of 'data' and the kernel match
mean_shifter = ms.MeanShift(kernel='multivariate_gaussian')
mean_shift_result = mean_shifter.cluster(data, kernel_bandwidth = [10,20,30])

Example

Plotting Into Graph

This is example using matplotlib to plot graphs

import mean_shift as ms
import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt('data.csv', delimiter=',')

mean_shifter = ms.MeanShift()
mean_shift_result = mean_shifter.cluster(data, kernel_bandwidth = 1)

original_points =  mean_shift_result.original_points
shifted_points = mean_shift_result.shifted_points
cluster_assignments = mean_shift_result.cluster_ids

x = original_points[:,0]
y = original_points[:,1]
Cluster = cluster_assignments
centers = shifted_points

fig = plt.figure()
ax = fig.add_subplot(111)
scatter = ax.scatter(x,y,c=Cluster,s=50)
for i,j in centers:
    ax.scatter(i,j,s=50,c='red',marker='+')
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.colorbar(scatter)

fig.savefig("mean_shift_result")

Image Segmentation

Mean shift can be used for image segmentation. Below is an example of an image being mean shift clustered in 3D RGB space, resulting in 7 clusters.

More Repositories

1

GradientDescentExample

Example demonstrating how gradient descent may be used to solve a linear regression problem
Python
535
star
2

palette-maker

Palette Maker is an interactive web tool that allows you to explore different approaches to extract color palettes from images
JavaScript
137
star
3

MeanShift_cpp

Mean shift clustering Implementation in C++
C++
90
star
4

react-redux-typescript

Project template for projects that use React, Redux, and TypeScript
TypeScript
38
star
5

CentralLimitTheoremDemo

Python
25
star
6

ruby-excel-library-examples

Example code for reading Excel files in ruby with different libraries.
Ruby
23
star
7

S3.FMA

Amazon S3 File Manager API in Python. S3.FMA is a thin wrapper around boto to perform specific high level file management tasks on an AWS S3 Bucket.
Python
20
star
8

swift-type-inference-bug

This project contains code that reproduces a bug that prevents the Swift compiler from being able to efficiently perform type inference on semi-simple dictionary or array literals.
Swift
17
star
9

beacon-scanner

Swift
14
star
10

ibeacon-finder

Simple iOS iBeacon app skeleton in Swift. Searches for an iBeacon in monitoring and ranging mode and prints results. This app contains no meaningful UI.
Swift
11
star
11

SwiftForecast

API Wrapper around The Dark Sky Forecast API in Swift
Swift
4
star
12

rickshaw_examples

HTML
4
star
13

react-native-demo-project

Simple cross platform app written with react native. Uses cross platform tabbed navigation, and loads and displays trending gifs from Giphy
JavaScript
2
star
14

spacemacs-configuration

Personal Spacemacs dotfile
Emacs Lisp
2
star
15

DataStructuresCSharp

Data structure implementations in C#
C#
1
star
16

algorithms

implementations for various algorithms
C#
1
star
17

AlgorithmsPython

Implementations of various algorithms in python
Python
1
star