QuestionAsk Questions?

Posted in : JSP-Servlet

The following is a JSP code for K-MEANS CLUSTERING .... My proect , titled "Pattern Analysis of Web Usage for System Improvement", involves the web log info from a database and then considering the attributes IP address and WEBPAGES,IN THE FORM OF NUMBERS(X,Y) for K-MEANS CLUSTERING... the output of this is used for WEB CACHE REPLACEMENT.. please let me knw,if the following code of K-MEANS CLUSTERING in JSP is proper and will take 2 dimenshional input(ip,webpages)... i m having hard time in converting ip and webpages into number for K-MEANS CLUSTERING...please reply.

<html> <head> <script type="text/javascript"> function kmeans( arrayToProcess, Clusters ) { var Groups = new Array(); var Centroids = new Array(); var oldCentroids = new Array(); var changed = false; // order the input array arrayToProcess.sort(function(a,b){return a - b}) // initialise group arrays for( initGroups=0; initGroups < Clusters; initGroups++ ) { Groups[initGroups] = new Array(); } // pick initial centroids initialCentroids=Math.round( arrayToProcess.length/(Clusters+1) ); for( i=0; i<Clusters; i++ ) { Centroids[i]=arrayToProcess[ (initialCentroids*(i+1)) ]; } do { for( j=0; j<Clusters; j++ ) { Groups[j] = []; } changed=false; for( i=0; i<arrayToProcess.length; i++ ) { Distance=-1; oldDistance=-1 for( j=0; j<Clusters; j++ ) { distance = Math.abs( Centroids[j]-arrayToProcess[i] ); if ( oldDistance==-1 ) { oldDistance = distance; newGroup = j; } else if ( distance <= oldDistance ) { newGroup=j; oldDistance = distance; } } Groups[newGroup].push( arrayToProcess[i] ); } oldCentroids=Centroids; for ( j=0; j<Clusters; j++ ) { total=0; newCentroid=0; for( i=0; i<Groups[j].length; i++ ) { total+=Groups[j][i]; } newCentroid=total/Groups[newGroup].length; Centroids[j]=newCentroid; } for( j=0; j<Clusters; j++ ) { if ( Centroids[j]!=oldCentroids[j] ) { changed=true; } } } while( changed==true ); return Groups; } </script> </head> <html>

View Answers

Related Tutorials/Questions & Answers: