<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>eric.ness.net &#187; C#</title>
	<atom:link href="http://eric.ness.net/archives/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://eric.ness.net</link>
	<description>...I never learned to read.</description>
	<lastBuildDate>Sat, 21 Jan 2012 05:27:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>K-Means Document Clustering</title>
		<link>http://eric.ness.net/archives/k-means-document-clustering/</link>
		<comments>http://eric.ness.net/archives/k-means-document-clustering/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 17:35:48 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Machine Learning]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://eric.ness.net/?p=357</guid>
		<description><![CDATA[K-Means Document Clustering in C#]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Feric.ness.net%2Farchives%2Fk-means-document-clustering%2F' data-shr_title='K-Means+Document+Clustering'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Feric.ness.net%2Farchives%2Fk-means-document-clustering%2F' data-shr_title='K-Means+Document+Clustering'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://eric.ness.net/wp-content/uploads/2009/11/kmeans.jpg"><img class="alignnone size-full wp-image-364" title="kmeans" src="http://eric.ness.net/wp-content/uploads/2009/11/kmeans.jpg" alt="" width="577" height="360" /></a></p>
<p>Using our <a href="http://eric.ness.net/archives/plotting-documents-words-using-latent-semantic-indexing/">previous example</a> as a basis to move to the next step let&#8217;s take a look at clustering using the <a href="http://en.wikipedia.org/wiki/K-means_clustering">K-Means</a> clustering algorithm to group the documents in to their appropriate categories.</p>
<p>In the paper â€œ<a href="http://lsa.colorado.edu/papers/JASIS.lsi.90.pdf">Indexing by Latent Semantic Analysis</a>â€ (Deerwester et al.) they have an example of 9 titles of different papers grouped in to two categories â€œhuman computer interactionâ€ &amp; â€œgraphs &amp; treesâ€. So far, we&#8217;ve used <a href="http://eric.ness.net/archives/singular-value-decomposition/">Singular Value Decomposition</a> (SVD) and <a href="http://eric.ness.net/archives/latent-semantic-indexing/">Latent Semantic Indexing</a> (LSI) to better understand the relationship of words and documents. In the <a href="http://eric.ness.net/archives/plotting-documents-words-using-latent-semantic-indexing/">last blog post</a> we then took the results in LSI to plot words and documents on a two dimensional Cartesian plane.</p>
<p>All of this is pretty interesting stuff in and of itself however, the next step really is to see which documents belong in each group. One way to do this is by using K-Means clustering.</p>
<blockquote><p>Simply speaking k-means clustering is an algorithm to classify or to group your objects based on attributes/features into K number of group. K is positive integer number. The grouping is done by minimizing the sum of squares of distances between data and the corresponding cluster centroid. Thus the purpose of K-mean clustering is to classify the data. [<a href="http://people.revoledu.com/kardi/tutorial/kMean/WhatIs.htm">Kardi Teknomo</a>]</p></blockquote>
<p>A big chunk of the code is built off of the same project we are working on. I am using <a href="http://sites.google.com/site/docaresh/">Aresh Saharkhiz</a> K-Means implementation in the project with some minor changes/refactoring done by me.</p>
<p>Let take a look at the code!</p>
<p>This first part is the display (an ASP.NET app.)</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Default.aspx.cs&quot; Inherits=&quot;LSITest._Default&quot; %&gt;
&lt;%@ Register Assembly=&quot;DundasWebChart&quot; Namespace=&quot;Dundas.Charting.WebControl&quot; TagPrefix=&quot;DCWC&quot; %&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;
&lt;head runat=&quot;server&quot;&gt;
    &lt;title&gt;LSI Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
    &lt;div&gt;
        &lt;DCWC:Chart ID=&quot;Chart1&quot; runat=&quot;server&quot; Height=&quot;400px&quot; Width=&quot;400px&quot;
            ImageType=&quot;Jpeg&quot;&gt;
            &lt;Legends&gt;
                &lt;DCWC:Legend Name=&quot;Default&quot; Alignment=&quot;Center&quot; Docking=&quot;Bottom&quot;&gt;&lt;/DCWC:Legend&gt;
            &lt;/Legends&gt;
            &lt;Titles&gt;
                &lt;DCWC:Title Name=&quot;Title1&quot;&gt;
                &lt;/DCWC:Title&gt;
            &lt;/Titles&gt;
            &lt;Series&gt;
                &lt;DCWC:Series Name=&quot;Series1&quot; ChartType=&quot;Point&quot; MarkerBorderColor=&quot;64, 64, 64&quot;
                    ShadowOffset=&quot;1&quot;&gt;
                &lt;/DCWC:Series&gt;
                &lt;DCWC:Series Name=&quot;Series2&quot; ChartType=&quot;Point&quot; MarkerBorderColor=&quot;64, 64, 64&quot;
                    ShadowOffset=&quot;1&quot;&gt;
                &lt;/DCWC:Series&gt;
                &lt;DCWC:Series Name=&quot;Series3&quot; ChartType=&quot;Point&quot; MarkerBorderColor=&quot;64, 64, 64&quot;
                    ShadowOffset=&quot;1&quot;&gt;
                &lt;/DCWC:Series&gt;
            &lt;/Series&gt;
            &lt;ChartAreas&gt;
                &lt;DCWC:ChartArea Name=&quot;Series2&quot;&gt;
                    &lt;axisy interval=&quot;0.5&quot; maximum=&quot;2&quot; minimum=&quot;-1&quot;&gt;
                        &lt;majorgrid linecolor=&quot;Gray&quot; linestyle=&quot;Dash&quot; /&gt;
                    &lt;/axisy&gt;
                    &lt;axisx interval=&quot;0.5&quot; maximum=&quot;2.5&quot; minimum=&quot;-0.5&quot;&gt;
                        &lt;majorgrid linecolor=&quot;Gray&quot; linestyle=&quot;Dash&quot; /&gt;
                    &lt;/axisx&gt;
                &lt;/DCWC:ChartArea&gt;
            &lt;/ChartAreas&gt;
        &lt;/DCWC:Chart&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>This is the code behind for the ASP.NET page. Because we are only dealing with two known categories K-Means is plotting out the two categories and if you wanted to do more you would definitely have to re-write the ColorCodeDocuments function.</p>
<pre class="brush: jscript; title: ; notranslate">
using System;
using System.Data;
using System.Drawing;
using System.Web.UI;
using Dundas.Charting.WebControl;

namespace LSITest
{
    public partial class _Default : Page
    {
        /// &lt;summary&gt;
        /// Handles the Load event of the Page control.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;sender&quot;&gt;The source of the event.&lt;/param&gt;
        /// &lt;param name=&quot;e&quot;&gt;The &lt;see cref=&quot;System.EventArgs&quot;/&gt; instance containing the event data.&lt;/param&gt;
        protected void Page_Load(object sender, EventArgs e)
        {
            // Perform LSI
            var mylsi = new lsi();
            mylsi.LSITest();
            double[,] myDocs = mylsi.MyDocs;

            // Plot Documents and the k-means
            const string distanceType = &quot;manhattan&quot;;
            PlotDocuments(myDocs, mylsi.MyDocRowCount);
            PlotKMeansPoints(myDocs, 2, distanceType);
            ColorCodeDocuments(distanceType);

            // If you want to plot the words just un-comment the next two lines
            //double[,] myWords = mylsi.MyWords;
            //PlotWords(myDocs, mylsi.MyWordsRowCount);

            // comment this line out to show words in legend
            Chart1.Series[&quot;Series2&quot;].ShowInLegend = false;
        }

        /// &lt;summary&gt;
        /// Plots the words.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;myWords&quot;&gt;My words.&lt;/param&gt;
        /// &lt;param name=&quot;myWordsRowCount&quot;&gt;My words row count.&lt;/param&gt;
        private void PlotWords(double[,] myWords, int myWordsRowCount)
        {
            for (int i = 0; i &lt; myWordsRowCount; i++)
            {
                Chart1.Series[&quot;Series2&quot;].Points.AddXY(myWords[i, 0], myWords[i, 1]);
            }

            // Set point colors and shapes
            Chart1.Series[&quot;Series2&quot;].LegendText = &quot;Words&quot;;
            Chart1.Series[&quot;Series2&quot;].Color = Color.Gray;
            Chart1.Series[&quot;Series2&quot;].MarkerStyle = MarkerStyle.Circle;
            Chart1.Series[&quot;Series2&quot;].MarkerSize = 6;
        }

        /// &lt;summary&gt;
        /// Plots the documents.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;myDocs&quot;&gt;My docs.&lt;/param&gt;
        /// &lt;param name=&quot;myDocRowCount&quot;&gt;My doc row count.&lt;/param&gt;
        private void PlotDocuments(double[,] myDocs, int myDocRowCount)
        {
            // Load documents
            for (int i = 0; i &lt; myDocRowCount; i++)
            {
                Chart1.Series[&quot;Series1&quot;].Points.AddXY(myDocs[i, 0], myDocs[i, 1]);
            }

            // Set point colors and shapes
            Chart1.Series[&quot;Series1&quot;].LegendText = &quot;Documents&quot;;
            Chart1.Series[&quot;Series1&quot;].Color = Color.Red;
            Chart1.Series[&quot;Series1&quot;].MarkerStyle = MarkerStyle.Diamond;
            Chart1.Series[&quot;Series1&quot;].MarkerSize = 12;
        }

        /// &lt;summary&gt;
        /// Plots the K means points.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;items&quot;&gt;The items.&lt;/param&gt;
        /// &lt;param name=&quot;k&quot;&gt;The k.&lt;/param&gt;
        /// &lt;param name=&quot;distanceType&quot;&gt;&lt;/param&gt;
        private void PlotKMeansPoints(double[,] items, int k, string distanceType)
        {
            ClusterCollection clusters = kmeans.ClusterDataSet(k, items, distanceType);

            for (int i = 0; i &lt; clusters.Count; i++)
            {
                Chart1.Series[&quot;Series3&quot;].Points.AddXY(clusters[i].ClusterMean[0], clusters[i].ClusterMean[1]);
            }

            // Set point colors and shapes
            Chart1.Series[&quot;Series3&quot;].LegendText = &quot;Cluster&quot;;
            Chart1.Series[&quot;Series3&quot;].Color = Color.Gold;
            Chart1.Series[&quot;Series3&quot;].MarkerStyle = MarkerStyle.Star6;
            Chart1.Series[&quot;Series3&quot;].MarkerSize = 18;
        }

        /// &lt;summary&gt;
        /// Colors the code documents.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;distanceType&quot;&gt;Type of the distance.&lt;/param&gt;
        private void ColorCodeDocuments(string distanceType)
        {
            var myDist = new similarity();

            // Extract data
            DataSet myDocs = Chart1.DataManipulator.ExportSeriesValues(&quot;Series1&quot;);
            DataSet myKMeansPoints = Chart1.DataManipulator.ExportSeriesValues(&quot;Series3&quot;);

            // Document counter
            int count = 0;

            // Get co-ordinates for k-means points
            double firstKMeansX = Convert.ToDouble(myKMeansPoints.Tables[0].Rows[0][&quot;X&quot;]);
            double firstKMeansY = Convert.ToDouble(myKMeansPoints.Tables[0].Rows[0][&quot;Y&quot;]);
            double secondKMeansX = Convert.ToDouble(myKMeansPoints.Tables[0].Rows[1][&quot;X&quot;]);
            double secondKMeansY = Convert.ToDouble(myKMeansPoints.Tables[0].Rows[1][&quot;Y&quot;]);

            foreach (DataRow docRow in myDocs.Tables[0].Rows)
            {
                // get co-ordinates for current doc
                double currentDocX = Convert.ToDouble(docRow[&quot;X&quot;]);
                double currentDocY = Convert.ToDouble(docRow[&quot;Y&quot;]);

                // load in to arrays
                double[] firstX = {currentDocX, currentDocY};
                double[] firstY = {firstKMeansX, firstKMeansY};
                double[] secondX = {currentDocX, currentDocY};
                double[] secondY = {secondKMeansX, secondKMeansY};

                // find the distance
                double firstDist = myDist.FindDistance(firstX, firstY, distanceType);
                double secondDist = myDist.FindDistance(secondX, secondY, distanceType);

                // Color accordingly
                Chart1.Series[&quot;Series1&quot;].Points[count].Color = firstDist &lt; secondDist ? Color.Blue : Color.Gray;
                count++;
            }
        }
    }
}
</pre>
<p>This is the K-Means class written by Aresh Saharkhiz with my changes</p>
<pre class="brush: jscript; title: ; notranslate">
/// Most of this code was written by Aresh Saharkhiz
/// Re-organized by me
/// See Code Project: http://www.codeproject.com/KB/recipes/K-Mean_Clustering.aspx
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;

namespace LSITest
{
    public class kmeans
    {
        /// &lt;summary&gt;
        /// Calculates The Mean Of A Cluster OR The Cluster Center
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;cluster&quot;&gt;
        /// A two-dimensional array containing a dataset of numeric values
        /// &lt;/param&gt;
        /// &lt;returns&gt;
        /// Returns an Array Defining A Data Point Representing The Cluster Mean or Centroid
        /// &lt;/returns&gt;
        public static double[] ClusterMean(double[,] cluster)
        {
            int rowCount = cluster.GetUpperBound(0) + 1;
            int fieldCount = cluster.GetUpperBound(1) + 1;
            var dataSum = new double[1,fieldCount];
            var centroid = new double[fieldCount];

            for (int j = 0; j &lt; fieldCount; j++)
            {
                for (int i = 0; i &lt; rowCount; i++)
                {
                    dataSum[0, j] = dataSum[0, j] + cluster[i, j];
                }

                centroid[j] = (dataSum[0, j]/rowCount);
            }

            return centroid;
        }

        /// &lt;summary&gt;
        /// Seperates a dataset into clusters or groups with similar characteristics
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;clusterCount&quot;&gt;The number of clusters or groups to form&lt;/param&gt;
        /// &lt;param name=&quot;data&quot;&gt;An array containing data that will be clustered&lt;/param&gt;
        /// &lt;param name=&quot;type&quot;&gt;&lt;/param&gt;
        /// &lt;returns&gt;A collection of clusters of data&lt;/returns&gt;
        public static ClusterCollection ClusterDataSet(int clusterCount, double[,] data, string type)
        {
            int rowCount = data.GetUpperBound(0) + 1;
            int fieldCount = data.GetUpperBound(1) + 1;
            int stableClustersCount = 0;
            double[] dataPoint;
            var random = new Random();
            Cluster cluster;
            var clusters = new ClusterCollection();
            var clusterNumbers = new ArrayList(clusterCount);
            var myDist = new similarity();

            while (clusterNumbers.Count &lt; clusterCount)
            {
                int clusterNumber = random.Next(0, rowCount - 1);

                if (!clusterNumbers.Contains(clusterNumber))
                {
                    cluster = new Cluster();
                    clusterNumbers.Add(clusterNumber);
                    dataPoint = new double[fieldCount];

                    for (int field = 0; field &lt; fieldCount; field++)
                    {
                        dataPoint.SetValue((data[clusterNumber, field]), field);
                    }

                    cluster.Add(dataPoint);
                    clusters.Add(cluster);
                }
            }

            while (stableClustersCount != clusters.Count)
            {
                stableClustersCount = 0;
                ClusterCollection newClusters = ClusterDataSet(clusters, data, type);

                for (int clusterIndex = 0; clusterIndex &lt; clusters.Count; clusterIndex++)
                {
                    if ((myDist.FindDistance(newClusters[clusterIndex].ClusterMean, clusters[clusterIndex].ClusterMean, type)) == 0)
                    {
                        stableClustersCount++;
                    }
                }

                clusters = newClusters;
            }

            return clusters;
        }

        /// &lt;summary&gt;
        /// Seperates a dataset into clusters or groups with similar characteristics
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;clusters&quot;&gt;A collection of data clusters&lt;/param&gt;
        /// &lt;param name=&quot;data&quot;&gt;An array containing data to b eclustered&lt;/param&gt;
        /// &lt;param name=&quot;type&quot;&gt;&lt;/param&gt;
        /// &lt;returns&gt;A collection of clusters of data&lt;/returns&gt;
        public static ClusterCollection ClusterDataSet(ClusterCollection clusters, double[,] data, string type)
        {
            double[] dataPoint;
            double firstClusterDistance = 0.0;
            int rowCount = data.GetUpperBound(0) + 1;
            int fieldCount = data.GetUpperBound(1) + 1;
            int position = 0;
            var myDist = new similarity();

            // create a new collection of clusters
            var newClusters = new ClusterCollection();

            for (int count = 0; count &lt; clusters.Count; count++)
            {
                var newCluster = new Cluster();
                newClusters.Add(newCluster);
            }

            if (clusters.Count &lt;= 0)
            {
                throw new SystemException(&quot;Cluster Count Cannot Be Zero!&quot;);
            }

            for (int row = 0; row &lt; rowCount; row++)
            {
                dataPoint = new double[fieldCount];

                for (int field = 0; field &lt; fieldCount; field++)
                {
                    dataPoint.SetValue((data[row, field]), field);
                }

                for (int cluster = 0; cluster &lt; clusters.Count; cluster++)
                {
                    double[] clusterMean = clusters[cluster].ClusterMean;

                    if (cluster == 0)
                    {
                        firstClusterDistance = myDist.FindDistance(dataPoint, clusterMean, type);
                        position = cluster;
                    }
                    else
                    {
                        double secondClusterDistance = myDist.FindDistance(dataPoint, clusterMean, type);

                        if (firstClusterDistance &gt; secondClusterDistance)
                        {
                            firstClusterDistance = secondClusterDistance;
                            position = cluster;
                        }
                    }
                }

                newClusters[position].Add(dataPoint);
            }

            return newClusters;
        }

        /// &lt;summary&gt;
        /// Converts the data table to array.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;table&quot;&gt;The table.&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static double[,] ConvertDataTableToArray(DataTable table)
        {
            int rowCount = table.Rows.Count;
            int fieldCount = table.Columns.Count;

            var dataPoints = new double[rowCount,fieldCount];

            for (int rowPosition = 0; rowPosition &lt; rowCount; rowPosition++)
            {
                DataRow row = table.Rows[rowPosition];

                for (int fieldPosition = 0; fieldPosition &lt; fieldCount; fieldPosition++)
                {
                    double fieldValue;
                    try
                    {
                        fieldValue = double.Parse(row[fieldPosition].ToString());
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.ToString());
                        throw new InvalidCastException(&quot;Invalid row at &quot; + rowPosition + &quot; and field &quot; + fieldPosition,
                                                       ex);
                    }

                    dataPoints[rowPosition, fieldPosition] = fieldValue;
                }
            }

            return dataPoints;
        }
    }

    /// &lt;summary&gt;
    /// A class containing a group of data with similar characteristics (cluster)
    /// &lt;/summary&gt;
    [Serializable]
    public class Cluster : CollectionBase
    {
        private double[] _clusterMean;
        private double[] _clusterSum;

        /// &lt;summary&gt;
        /// The sum of all the data in the cluster
        /// &lt;/summary&gt;
        public double[] ClusterSum
        {
            get { return _clusterSum; }
        }

        /// &lt;summary&gt;
        /// The mean of all the data in the cluster
        /// &lt;/summary&gt;
        public double[] ClusterMean
        {
            get
            {
                for (int count = 0; count &lt; this[0].Length; count++)
                {
                    _clusterMean[count] = (_clusterSum[count]/List.Count);
                }

                return _clusterMean;
            }
        }

        /// &lt;summary&gt;
        /// Returns the one dimensional array data located at the index
        /// &lt;/summary&gt;
        public virtual double[] this[int index]
        {
            get
            {
                //return the Neuron at IList[index]
                return (double[]) List[index];
            }
        }

        /// &lt;summary&gt;
        /// Adds a single dimension array data to the cluster
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;data&quot;&gt;A 1-dimensional array containing data that will be added to the cluster&lt;/param&gt;
        public virtual void Add(double[] data)
        {
            List.Add(data);

            if (List.Count == 1)
            {
                _clusterSum = new double[data.Length];

                _clusterMean = new double[data.Length];
            }

            for (int count = 0; count &lt; data.Length; count++)
            {
                _clusterSum[count] = _clusterSum[count] + data[count];
            }
        }
    }

    /// &lt;summary&gt;
    /// A collection of Cluster objects or Clusters
    /// &lt;/summary&gt;
    [Serializable]
    public class ClusterCollection : CollectionBase
    {
        /// &lt;summary&gt;
        /// Returns the Cluster at this index
        /// &lt;/summary&gt;
        public virtual Cluster this[int index]
        {
            get
            {
                //return the Neuron at IList[index]
                return (Cluster) List[index];
            }
        }

        /// &lt;summary&gt;
        /// Adds a Cluster to the collection of Clusters
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;cluster&quot;&gt;A Cluster to be added to the collection of clusters&lt;/param&gt;
        public virtual void Add(Cluster cluster)
        {
            List.Add(cluster);
        }
    }
}
</pre>
<p>Here is the similarity class than can calculate Euclidean, Manhattan, Chebyshev, Minkowski distances</p>
<pre class="brush: jscript; title: ; notranslate">
/// Most of this code was written by Aresh Saharkhiz
/// Re-organized by me
/// See Code Project: http://www.codeproject.com/KB/recipes/Quantitative_Distances.aspx
using System;

namespace LSITest
{
    public class similarity
    {
        /// &lt;summary&gt;
        /// Finds the distance.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;x&quot;&gt;The x.&lt;/param&gt;
        /// &lt;param name=&quot;y&quot;&gt;The y.&lt;/param&gt;
        /// &lt;param name=&quot;type&quot;&gt;The type.&lt;/param&gt;
        /// &lt;param name=&quot;distanceType&quot;&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public double FindDistance(double[] x, double[] y, string distanceType)
        {
            double distance;

            switch (distanceType.ToLower())
            {
                case &quot;euclidean&quot;:
                    distance = EuclideanDistance(x, y);
                    break;
                case &quot;manhattan&quot;:
                    distance = ManhattanDistance(x, y);
                    break;
                case &quot;minkowski&quot;:
                    distance = MinkowskiDistance(x, y, 1);
                    break;
                case &quot;chebyshev&quot;:
                    distance = ChebyshevDistance(x, y);
                    break;
                default:
                    distance = 0.0;
                    break;
            }

            return distance;
        }

        /// &lt;summary&gt;
        /// Finds the Euclideans distance.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;x&quot;&gt;The x.&lt;/param&gt;
        /// &lt;param name=&quot;y&quot;&gt;The y.&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public double EuclideanDistance(double[] x, double[] y)
        {
            double sum = 0.0;

            if (x.GetUpperBound(0) != y.GetUpperBound(0))
            {
                throw new ArgumentException(&quot;the number of elements in x must match the number of elements in y&quot;);
            }

            int count = x.Length;

            for (int i = 0; i &lt; count; i++)
            {
                sum += Math.Pow(Math.Abs(x[i] - y[i]), 2);
            }

            double distance = Math.Sqrt(sum);
            return distance;
        }

        /// &lt;summary&gt;
        /// Finds Manhattan distance.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;x&quot;&gt;The x.&lt;/param&gt;
        /// &lt;param name=&quot;y&quot;&gt;The y.&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public double ManhattanDistance(double[] x, double[] y)
        {
            double sum = 0.0;

            if (x.GetUpperBound(0) != y.GetUpperBound(0))
            {
                throw new ArgumentException(&quot;the number of elements in x must match the number of elements in y&quot;);
            }

            int count = x.Length;

            for (int i = 0; i &lt; count; i++)
            {
                sum += Math.Abs(x[i] - y[i]);
            }

            double distance = sum;
            return distance;
        }

        /// &lt;summary&gt;
        /// Finds Chebyshevs distance.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;x&quot;&gt;The x.&lt;/param&gt;
        /// &lt;param name=&quot;y&quot;&gt;The y.&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static double ChebyshevDistance(double[] x, double[] y)
        {
            if (x.GetUpperBound(0) != y.GetUpperBound(0))
            {
                throw new ArgumentException(&quot;the number of elements in x must match the number of elements in y&quot;);
            }
            int count = x.Length;
            var newData = new double[count];

            for (int i = 0; i &lt; count; i++)
            {
                newData[i] = Math.Abs(x[i] - y[i]);
            }
            double max = double.MinValue;

            foreach (double num in newData)
            {
                if (num &gt; max)
                {
                    max = num;
                }
            }
            return max;
        }

        /// &lt;summary&gt;
        /// Finds Minkowskis distance.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;x&quot;&gt;The x.&lt;/param&gt;
        /// &lt;param name=&quot;y&quot;&gt;The y.&lt;/param&gt;
        /// &lt;param name=&quot;order&quot;&gt;The order.&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public double MinkowskiDistance(double[] x, double[] y, double order)
        {
            double sum = 0.0;

            if (x.GetUpperBound(0) != y.GetUpperBound(0))
            {
                throw new ArgumentException(&quot;the number of elements in x must match the number of elements in y&quot;);
            }
            int count = x.Length;

            for (int i = 0; i &lt; count; i++)
            {
                sum = sum + Math.Pow(Math.Abs(x[i] - y[i]), order);
            }

            double distance = Math.Pow(sum, (1 / order));
            return distance;
        }
    }
}
</pre>
<p>And finally the same LSI class used in the previous examples.</p>
<pre class="brush: jscript; title: ; notranslate">
using System;
using SmartMathLibrary;

namespace LSITest
{
    public class lsi
    {
        // this returns the formated html results
        public int MyDocColumnCount;
        public int MyDocRowCount;
        public double[,] MyDocs;
        public double[,] MyWords;
        public int MyWordsColumnCount;
        public int MyWordsRowCount;
        public string ToPrint;

        /// &lt;summary&gt;
        /// LISs the test.
        /// &lt;/summary&gt;
        public void LSITest()
        {
            //Create Matrix
            var testArray = new double[,]
                                {
                                    {1, 0, 0, 1, 0, 0, 0, 0, 0},
                                    {1, 0, 1, 0, 0, 0, 0, 0, 0},
                                    {1, 1, 0, 0, 0, 0, 0, 0, 0},
                                    {0, 1, 1, 0, 1, 0, 0, 0, 0},
                                    {0, 1, 1, 2, 0, 0, 0, 0, 0},
                                    {0, 1, 0, 0, 1, 0, 0, 0, 0},
                                    {0, 1, 0, 0, 1, 0, 0, 0, 0},
                                    {0, 0, 1, 1, 0, 0, 0, 0, 0},
                                    {0, 1, 0, 0, 0, 0, 0, 0, 1},
                                    {0, 0, 0, 0, 0, 1, 1, 1, 0},
                                    {0, 0, 0, 0, 0, 0, 1, 1, 1},
                                    {0, 0, 0, 0, 0, 0, 0, 1, 1}
                                };

            // Load array in to Matrix
            var a = new Matrix(testArray);

            // print original matrix
            PrintMatrix(a);

            // preform Latent Semantic Indexing
            GetDocumentWordPlots(a);
        }

        /// &lt;summary&gt;
        /// Prints the matrix.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;myMatrix&quot;&gt;My matrix.&lt;/param&gt;
        private void PrintMatrix(IMatrix myMatrix)
        {
            ToPrint += &quot;&lt;br /&gt;&lt;br /&gt;&quot;;

            for (int i = 0; i &lt; myMatrix.Rows; i++)
            {
                for (int j = 0; j &lt; myMatrix.Columns; j++)
                {
                    ToPrint += String.Format(&quot;{0:0.##}&quot;, myMatrix.MatrixData[i, j]) + &quot;\t&quot;;
                }
                ToPrint += &quot;&lt;br /&gt;&quot;;
            }
        }

        /// &lt;summary&gt;
        /// Gets the document word plots.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;myMatrix&quot;&gt;My matrix.&lt;/param&gt;
        private void GetDocumentWordPlots(Matrix myMatrix)
        {
            // Run single value decomposition
            var svd = new SingularValueDecomposition(myMatrix);
            svd.ExecuteDecomposition();

            // Put components into individual matrices
            Matrix wordVector = svd.U.Copy();
            Matrix sigma = svd.S.ToMatrix();
            Matrix documentVector = svd.V.Copy();

            // get value of k
            // you can also manually set the value of k
            var k = (int) Math.Floor(Math.Sqrt(myMatrix.Columns));

            // reduce the vectors
            Matrix reducedWordVector = CopyMatrix(wordVector, wordVector.Rows, k - 1);
            Matrix reducedSigma = CreateSigmaMatrix(sigma, k - 1, k - 1);
            Matrix reducedDocumentVector = CopyMatrix(documentVector, documentVector.Rows, k - 1);

            // Recalculate the matrix
            Matrix docs = reducedDocumentVector*reducedSigma;
            Matrix words = reducedWordVector*reducedSigma;

            // Fill doc plot locations
            MyDocs = new double[docs.Rows,docs.Columns];
            for (int i = 0; i &lt; docs.Rows; i++)
            {
                for (int j = 0; j &lt; docs.Columns; j++)
                {
                    MyDocs[i, j] = docs.MatrixData[i, j];
                }
            }

            // Fill word plot locations
            MyWords = new double[words.Rows,words.Columns];
            for (int i = 0; i &lt; words.Rows; i++)
            {
                for (int j = 0; j &lt; words.Columns; j++)
                {
                    MyWords[i, j] = words.MatrixData[i, j];
                }
            }

            // Set counts for charts
            MyDocRowCount = docs.Rows;
            MyWordsRowCount = words.Rows;

            PrintMatrix(docs);
            PrintMatrix(words);
        }

        /// &lt;summary&gt;
        /// Creates the sigma matrix.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;matrix&quot;&gt;The matrix.&lt;/param&gt;
        /// &lt;param name=&quot;rowEnd&quot;&gt;The row end.&lt;/param&gt;
        /// &lt;param name=&quot;columnEnd&quot;&gt;The column end.&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        private static Matrix CreateSigmaMatrix(IMatrix matrix, int rowEnd, int columnEnd)
        {
            var copyMatrix = new Matrix(rowEnd, columnEnd);

            for (int i = 0; i &lt; columnEnd; i++)
            {
                copyMatrix.MatrixData[i, i] = matrix.MatrixData[i, 0];
            }

            return copyMatrix;
        }

        /// &lt;summary&gt;
        /// Copies the matrix.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;myMatrix&quot;&gt;My matrix.&lt;/param&gt;
        /// &lt;param name=&quot;rowEnd&quot;&gt;The row end.&lt;/param&gt;
        /// &lt;param name=&quot;columnEnd&quot;&gt;The column end.&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        private static Matrix CopyMatrix(IMatrix myMatrix, int rowEnd, int columnEnd)
        {
            var copyMatrix = new Matrix(rowEnd, columnEnd);

            for (int i = 0; i &lt; rowEnd; i++)
            {
                for (int j = 0; j &lt; columnEnd; j++)
                {
                    copyMatrix.MatrixData[i, j] = myMatrix.MatrixData[i, j];
                }
            }

            return copyMatrix;
        }
    }
}
</pre>
<p>And what do the results look like?</p>
<p><a href="http://eric.ness.net/wp-content/uploads/2009/11/kmeansresults.jpg"><img class="alignnone size-full wp-image-361" style="margin-left: 100px; margin-right: 100px;" title="kmeansresults" src="http://eric.ness.net/wp-content/uploads/2009/11/kmeansresults.jpg" alt="kmeansresults" width="400" height="400" /></a></p>
<p>As you can see the K-Means clustering algorithm correctly grouped the documents in the appropriate categories.</p>
<p>Recommended reading and thanks goes to <a href="http://www.codeproject.com/KB/recipes/K-Mean_Clustering.aspx">Aresh Saharkhiz</a> for sharing his implementation of K-Means Clustering.</p>
<div class="shr-publisher-357"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://eric.ness.net/archives/k-means-document-clustering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problems with Html.DropDownList</title>
		<link>http://eric.ness.net/archives/problems-with-htmldropdownlist/</link>
		<comments>http://eric.ness.net/archives/problems-with-htmldropdownlist/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 03:43:24 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://eric.ness.net/?p=128</guid>
		<description><![CDATA[For some reason my drop down list is not retaining it's selected value - I know I am missing something simple here. ]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Feric.ness.net%2Farchives%2Fproblems-with-htmldropdownlist%2F' data-shr_title='Problems+with+Html.DropDownList'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Feric.ness.net%2Farchives%2Fproblems-with-htmldropdownlist%2F' data-shr_title='Problems+with+Html.DropDownList'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://eric.ness.net/wp-content/uploads/2009/02/rubikshat.jpg"><img class="alignnone size-full wp-image-131" title="rubikshat" src="http://eric.ness.net/wp-content/uploads/2009/02/rubikshat.jpg" alt="" width="577" height="360" /></a></p>
<p>I guess I should say that this issue is not as bad as the cat with the Rubiks Cube but, I like the pic.</p>
<p>I posted this on a couple of boards with no real answer so I thought I would post it here to see if anyone can give me any insight in to how to approach this problem.</p>
<p>Controllers:</p>
<p>public ActionResult Test()<br />
{<br />
ViewData[&quot;MonitoringType&quot;] = new SelectList(myModel.GetMonitoringType(), &quot;Category&quot;, &quot;Category&quot;);<br />
return View();<br />
}</p>
<p>[AcceptVerbs(HttpVerbs.Post)]<br />
public ActionResult Test(FormCollection formValues)<br />
{<br />
ViewData[&quot;MonitoringType&quot;] = new SelectList(myModel.GetMonitoringType(), &quot;Category&quot;, &quot;Category&quot;, formValues[&quot;MonitoringType&quot;]);<br />
return View();<br />
}</p>
<p>And not surprisingly this works:</p>
<p>&lt;%= Html.DropDownList(&quot;MonitoringType&quot;)%&gt;</p>
<p>and this doesn&#8217;t</p>
<p>&lt;%= Html.DropDownList(&quot;MonitoringType&quot;, (SelectList)ViewData[&quot;MonitoringType&quot;],new {style = &quot;width: 300px;&quot;})%&gt;</p>
<p>Currently, my workaround is to implement the first version and then use jQuery add attributes on the fly.</p>
<div class="shr-publisher-128"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://eric.ness.net/archives/problems-with-htmldropdownlist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Euclidean Distance Score</title>
		<link>http://eric.ness.net/archives/euclidean-distance-score/</link>
		<comments>http://eric.ness.net/archives/euclidean-distance-score/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 02:14:19 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Machine Learning]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://eric.ness.net/?p=81</guid>
		<description><![CDATA[I am currently reading Toby Segaran's book "Programming Collective Intelligence" and one of the first topics it covers is how do you determine of similar two people are.]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Feric.ness.net%2Farchives%2Feuclidean-distance-score%2F' data-shr_title='Euclidean+Distance+Score'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Feric.ness.net%2Farchives%2Feuclidean-distance-score%2F' data-shr_title='Euclidean+Distance+Score'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://eric.ness.net/wp-content/uploads/2008/10/euclidean.jpg"><img class="alignnone size-full wp-image-82" title="euclidean" src="http://eric.ness.net/wp-content/uploads/2008/10/euclidean.jpg" alt="" width="577" height="360" /></a></p>
<p>I am currently reading Toby Segaran&#8217;s book &#8220;Programming Collective Intelligence&#8221; and one of the first topics it covers is how do you determine of similar two people are.</p>
<p>One approach is to use the Euclidean Distance Score. Arun Vijayan C has an excellent power point presentation &#8220;Finding more people like you&#8221; &#8211; on this topic:</p>
<div id="__ss_407295" style="width: 425px; text-align: left;"><a style="font: 14px Helvetica,Arial,Sans-serif; display: block; margin: 12px 0 3px 0; text-decoration: underline;" title="Finding more people like you" href="http://www.slideshare.net/arunv/finding-more-people-like-you?type=powerpoint">Finding more people like you</a><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="355" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slideshare.net/swf/ssplayer2.swf?doc=finding-people-like-you-1210843795287894-8&amp;stripped_title=finding-more-people-like-you" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="355" src="http://static.slideshare.net/swf/ssplayer2.swf?doc=finding-people-like-you-1210843795287894-8&amp;stripped_title=finding-more-people-like-you" allowscriptaccess="always" allowfullscreen="true"></embed></object>&nbsp;</p>
<div style="font-size: 11px; font-family: tahoma,arial; height: 26px; padding-top: 2px;">View SlideShare <a style="text-decoration: underline;" title="View Finding more people like you on SlideShare" href="http://www.slideshare.net/arunv/finding-more-people-like-you?type=powerpoint">presentation</a> or <a style="text-decoration: underline;" href="http://www.slideshare.net/upload?type=powerpoint">Upload</a> your own. (tags: <a style="text-decoration: underline;" href="http://slideshare.net/tag/socialnetwork">socialnetwork</a> <a style="text-decoration: underline;" href="http://slideshare.net/tag/compare">compare</a>)</div>
</div>
<p>I then wrote up some quick code in C# that uses the values in Arun&#8217;s presentation:</p>
<pre class="brush: jscript; title: ; notranslate">
// Euclidean Distance Score

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main()
        {
            // Load People and Values
            var myP = new List
                          {
                              new People(&quot;John&quot;, 1.5, 4),
                              new People(&quot;Ravi&quot;, 4.5, 1.5),
                              new People(&quot;Kiran&quot;, 1, 3.5),
                              new People(&quot;Deepti&quot;, 3, 5)
                          };

            // Print header
            Console.WriteLine(&quot;People And Scores&quot;);
            Console.WriteLine(&quot;###################&quot;);
            Console.WriteLine();

            // Loop through people and values
            foreach (People people in myP)
            {
                Console.WriteLine(people.Name + &quot;\t&quot; + people.xScore + &quot;\t&quot; + people.yScore);
            }

            // Print Distance And Value Headers
            Console.WriteLine();
            Console.WriteLine(&quot;Distance Comparison&quot;);
            Console.WriteLine(&quot;###################&quot;);
            Console.WriteLine();

            // Loop through people and scores
            int myCount = 1;
            for (int i = 0; i &lt; myP.Count; i++)
            {
                for (int j = myCount; j &lt; myP.Count; j++)
                {
                    // Euclidean Distance Score
                    // Sqrt( (x1-x2)^2 + (y1+y2)^2)
                    Console.WriteLine(myP[i].Name + &quot;\t&quot; + myP[j].Name + &quot;:\t&quot; +
                                      Math.Sqrt(Math.Pow(myP[i].xScore - myP[j].xScore, 2) +
                                                Math.Pow(myP[i].yScore - myP[j].yScore, 2)).ToString(&quot;0.##&quot;));
                }

                // Skip to the next guy
                myCount++;
            }

            // Print Closer
            Console.WriteLine();
            Console.WriteLine(&quot;Press enter to continue...&quot;);
            Console.ReadLine();
        }
    }

    internal class People
    {
        public string Name;
        public double xScore;
        public double yScore;

        public People(string _Name, double _xScore, double _yScore)
        {
            Name = _Name;
            xScore = _xScore;
            yScore = _yScore;
        }
    }
}
</pre>
<div class="shr-publisher-81"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://eric.ness.net/archives/euclidean-distance-score/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cheap GPS and Code Project Tutorial</title>
		<link>http://eric.ness.net/archives/cheap-gps-and-code-project-tutorial/</link>
		<comments>http://eric.ness.net/archives/cheap-gps-and-code-project-tutorial/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 19:19:10 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://eric.ness.net/blog/archives/cheap-gps-and-code-project-tutorial/</guid>
		<description><![CDATA[I came across a tutorial today on Code Project called "Mapping with a GPS and C#". I then found this cheap usb GPS receiver on Amazon for $29. And so begins a new little project I would like to try out.]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Feric.ness.net%2Farchives%2Fcheap-gps-and-code-project-tutorial%2F' data-shr_title='Cheap+GPS+and+Code+Project+Tutorial+'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Feric.ness.net%2Farchives%2Fcheap-gps-and-code-project-tutorial%2F' data-shr_title='Cheap+GPS+and+Code+Project+Tutorial+'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I came across a tutorial today on <a title="Code Project" href="http://www.codeproject.com">Code Project</a> called &#8220;<a title="Mapping with a GPS and C#" href="http://www.codeproject.com/KB/cs/GpsMapping.aspx?fid=987404&amp;df=90&amp;mpp=25&amp;noise=3&amp;sort=Position&amp;view=Quick">Mapping with a GPS and C#</a>&#8220;. I then found this <a title="cheap usb GPS receiver" href="http://www.amazon.com/UT-41-USB-12-Channel-NMEA-0183-Receiver/dp/B000G6TYC8">cheap usb GPS receiver</a> on Amazon for $29. And so begins a new little project I would like to try out.</p>
<p><strong>The NMEA 0183 Stream</strong></p>
<p>What isn&#8217;t mentioned in the tutorial in any great depth is that NMEA stream coming from the reciever actually holds a lot more information than just the simple logitude and latitude of the reciever. Here is a list of some of the highlights:</p>
<ul>
<li class="sentence">$GPAAM &#8211; Waypoint Arrival Alarm</li>
<li class="sentence">$GPBOD &#8211; Bearing, Origin to Destination</li>
<li class="sentence">$GPBWW &#8211; Bearing, Waypoint to Waypoint</li>
<li class="sentence">$GPGGA &#8211; Global Positioning System Fix Data</li>
<li class="sentence">$GPGLL &#8211; Geographic Position, Latitude/Longitude</li>
<li class="sentence">$GPGSA &#8211; GPS DOP and Active Satellites</li>
<li class="sentence">$GPGST &#8211; GPS Pseudorange Noise Statistics</li>
<li class="sentence">$GPGSV &#8211; GPS Satellites in View</li>
<li class="sentence">$GPHDG &#8211; Heading, Deviation &amp; Variation</li>
<li class="sentence">$GPHDT &#8211; Heading, True</li>
<li class="sentence">$GPRMB &#8211; Recommended Minimum Navigation Information</li>
<li class="sentence">$GPRMC &#8211; Recommended Minimum Specific GPS/TRANSIT Data</li>
<li class="sentence">$GPRTE &#8211; Routes</li>
<li class="sentence">$GPVTG &#8211; Track Made Good and Ground Speed</li>
<li class="sentence">$GPWCV &#8211; Waypoint Closure Velocity</li>
<li class="sentence">$GPWNC &#8211; Distance, Waypoint to Waypoint</li>
<li class="sentence">$GPWPL &#8211; Waypoint Location</li>
<li class="sentence">$GPXTE &#8211; Cross-Track Error, Measured</li>
<li class="sentence">$GPXTR &#8211; Cross-Track Error, Dead Reckoning</li>
<li class="sentence">$GPZDA &#8211; UTC Date/Time and Local Time Zone Offset</li>
<li class="sentence">$GPZFO &#8211; UTC and Time from Origin Waypoint</li>
<li class="sentence">$GPZTG &#8211; UTC and Time to Destination Waypoint</li>
</ul>
<blockquote><p>[via <a href="http://www.scientificcomponent.com/nmea0183.htm">link</a> and the complete list <a href="http://home.mira.net/~gnb/gps/nmea.html">here</a>]</p></blockquote>
<p>So I was thinking about putting together a little bigger application together and utilizing more of the NMEA stream that would sync up with a website.</p>
<div class="shr-publisher-12"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://eric.ness.net/archives/cheap-gps-and-code-project-tutorial/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

