Gary Chambers avatar Gary Chambers committed 5ad5aeb

Updated model to use ES5 array methods.

Comments (0)

Files changed (1)

 	 */
 	extractFeatures = function extractFeatures( points )
 	{
-		var i, j,
-
-			features,
-
-			feature,
-			point;
-
-		i = points.length;
+		var features;
 		features = {};
 
-		pointLoop:
-		while( i-- )
+		points.forEach(function( point )
 		{
-			point = points[ i ];
-
-			featureLoop:
-			for( j in point.keys() )
+			point.keys().forEach(function( key )
 			{
-				if( !features.hasOwnProperty(j) )
+				var feature;
+
+				if( !features.hasOwnProperty(key) )
 				{
-					switch( typeof point.get(j) )
+					switch( typeof point.get(key) )
 					{
 						case "number":
-							feature = new kNN.feature.Numerical( j );
+							feature = new kNN.feature.Numerical( key );
 							break;
 
 						case "string":
-							feature = new kNN.feature.Category( j );
+							feature = new kNN.feature.Category( key );
 							break;
 
 						default:
-							continue featureLoop;
+							return;
 					}
 
-					features[ j ] = feature;
+					features[ key ] = feature;
 				}
 
-				features[ j ].checkValue( point.get(j) );
-			}
-		}
+				features[ key ].checkValue( point.get(key) );
+			});
+		});
 
 		return features;
 	};
 	/**
 	 * Predict the category for a point
 	 * based on its K-nearest neighbors
+	 *
 	 * @param  {Number} K How many points to compare
 	 * @param  {Point} point
 	 * @param  {Array} neighbors An array of points
 			i, n;
 
 		categories = {};
+		bestScore = 0;
 		i = 0;
 
 		for( ; i < K; i++ )
 		{
-			n = neighbors[i];
+			n = neighbors[i][0];
 
 			if( !categories.hasOwnProperty(n.category) )
 			{
 	 */
 	Model = function Model( data )
 	{
-		var i;
+		var self;
+		self = this;
 
 		this.points = [];
 
-		i = data.length;
-
-		while( i-- )
+		data.forEach(function( item )
 		{
-			this.points.push(
-				new kNN.Point( data[i][0], data[i][1] )
+			self.points.push(
+				new kNN.Point( item[0], item[1] )
 			);
-		}
+		});
 
 		this.features = extractFeatures( this.points );
 	};
 	 * Classifies a point based on the
 	 * dimensions of the training data.
 	 *
-	 * @param {Number} K How many adjacent points to compare
 	 * @param {Point} point
+	 * @param {Number} K Number of points to compare
 	 * @return {String}
 	 */
-	Model.prototype.classify = function classify( K, point )
+	Model.prototype.classify = function classify( point, K )
 	{
 		var distance,
 			points,
+			self;
 
-			f, i, j, p;
-
-		i = this.points.length;
-		j = this.features.length;
-
+		self = this;
 		points = [];
 
-		while( i-- )
+		this.points.forEach(function( p )
 		{
-			p = this.points[ i ];
+			var key;
+
 			distance = 0;
 
-			for( j in this.features )
+			for( key in self.features )
 			{
-				f = this.features[ j ];
-
 				distance += Math.pow(
-					f.calcDistance( point, p ),
+					self.features[ key ].calcDistance( point, p ),
 					2
 				);
 			}
 
 			distance = Math.sqrt( distance );
 			points.push( [p, distance] );
-		}
+		});
 
 		points = sortPoints( points );
 		return predictCategory( K, point, points );
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.