⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kmeans.php

📁 K means clustering using php.
💻 PHP
字号:
<html><body>
<?
$i=0;
$j=0;
$e[$i][$j]=2;
$j=$j+1;
$e[$i][$j]=4;
$j=$j+1;
$e[$i][$j]=5;
$j=$j+1;
$e[$i][$j]=6;
$j=$j+1;
$e[$i][$j]=1;
$j=$j+1;
$e[$i][$j]=2;
$j=$j+1;
$e[$i][$j]=7;
$j=$j+1;
$e[$i][$j]=2;
$i=$i+1;
$j=0;
$e[$i][$j]=1;
$j=$j+1;
$e[$i][$j]=1;
$j=$j+1;
$e[$i][$j]=1;
$j=$j+1;
$e[$i][$j]=1;
$j=$j+1;
$e[$i][$j]=2;
$j=$j+1;
$e[$i][$j]=2;
$j=$j+1;
$e[$i][$j]=2;
$j=$j+1;
$e[$i][$j]=3;

$h1=0;
$h2=0;
$c1=0;
$c2=7;
$ac1[0]=2;
$ac1[1]=1;
$ac2[0]=2;
$ac2[1]=3;
$nc1[0]='nil';
$nc1[1]='nil';
$nc2[0]='nil';
$nc2[1]='nil';
$cont=0;

echo '<table border="1"><tr><td>Echantillon</td><td>Profondeur</td><td>Couleur</td></tr>';
for($g=0;$g<=$j;$g++)
 {
  echo '<tr><td>'.($g+1).'</td>';
  for($f=0;$f<=$i;$f++)
   {
    echo '<td>'.$e[$f][$g].'</td>';
   }
  echo '</tr>';
 }
echo '</table>';
echo '<br><font color="sky blue" size="5"><b><u>Centres initiaux</u></b>:C0('.$ac1[0].','.$ac1[1].') & C1('.$ac2[0].','.$ac2[1].')</font><br><br>';


//as long as the centers obtained doesn't change do those steps
while($ac1[0] != $nc1[0] or $ac1[1] != $nc1[1] or $ac2[0] != $nc2[0] or $ac2[1] != $nc2[1])
 {
  if($cont != 0)
   {
    $ac1[0]=$nc1[0];
    $ac1[1]=$nc1[1];
    $ac2[0]=$nc2[0];
    $ac2[1]=$nc2[1];
   }

  //premiere iteration
  //calcul de distance euclidienne

  //1.initialisation ds distances sans racine carre a 0
  $dsrc1=array();
  $dsrc2=array();
  for($t=0;$t<$j+1;$t++)
   {
    $dsrc1[$t]=0;
    $dsrc2[$t]=0;
   }




  //calcul de distances sans racine carre entre chacun des centres intiaux et les autres points ou echantillons pour chacun des echantillons
  for($t=0;$t<$j+1;$t++)
   {
    for($s=0;$s<$i+1;$s++)
     {
      $dsrc1[$t]=$dsrc1[$t]+(($e[$s][$t]-$ac1[$s])*($e[$s][$t]-$ac1[$s]));
      $dsrc2[$t]=$dsrc2[$t]+(($e[$s][$t]-$ac2[$s])*($e[$s][$t]-$ac2[$s]));
     }
   }


  for($t=0;$t<$j+1;$t++)
   {
    //calculte real distance with sqrt beteen points and centers
    $dsrc1[$t]=sqrt($dsrc1[$t]);
    $dsrc2[$t]=sqrt($dsrc2[$t]);
    $fr[$t]=min($dsrc1[$t],$dsrc2[$t]);
    if($fr[$t]== $dsrc1[$t])
     {
      $min[$t]=0;
     }
    else
     {
      $min[$t]=1;
     }
   }

  $nc1=array();
  for($f=0;$f<$i;$f++)
   {
    $nc1[$f]=0;
    $nc2[$f]=0;
   }

  $m1=0;
  $m2=0;
  for($t=0;$t<=$j;$t++)
   {
    for($f=0;$f<=$i;$f++)
     {
      if($min[$t]==0)
       {
        $nc1[$f]=$nc1[$f]+$e[$f][$t];
       }
       if($t==0)
       {
        $nc2[$f]=0;
       }
      if($min[$t]==1)
       {
        $nc2[$f]=$nc2[$f]+$e[$f][$t];
       }
     }
   }


  for($t=0;$t<=$j;$t++)
   {

   //search of elements number near to each center
    if($min[$t]==0)
     {
      $m1++;
     }
    if($min[$t]==1)
     {
      $m2++;
     }
   }

  //get new centers vaues
  for($f=0;$f<=$i;$f++)
   {
    if($m1 != 0)
     {
      $nc1[$f]=$nc1[$f]/$m1;
     }
    if($m2 != 0)
     {
      $nc2[$f]=$nc2[$f]/$m2;
     }
   }



 if($ac1[0] != $nc1[0] or $ac1[1] != $nc1[1] or $ac2[0] != $nc2[0] or $ac2[1] != $nc2[1])
  {
  echo '<font color="turquoise" size="4.5"><b><u>Tableau de distances avec les centres et de groupes obtenus:</b></u></font><br><br><table border="1"><tr><td>c0</td><td>c1</td><td>min</td></tr><tr>';
  for($t=0;$t<$j+1;$t++)
   {
     echo '<td>'.$dsrc1[$t].'</td>';
     echo '<td>'.$dsrc2[$t].'</td>';
     echo '<td>'.$min[$t].'</td></tr><tr>';
   }
  echo '</tr></table><br>';
  $cont++;



    echo '<font color="teal" size="4.5"><b><u>nouveau c0:</u></b></font> (';
  for($f=0;$f<=$i;$f++)
   {
    echo $nc1[$f];
    if($f < $i)
     {
      echo '&nbsp &nbsp &nbsp';
     }
   }
  echo ')';

    echo '<br><font color="teal" size="4.5"><b><u>nouveau c1 :</u></b></font> (';
  for($f=0;$f<=$i;$f++)
   {
    echo $nc2[$f];
    if($f < $i)
     {
      echo '&nbsp &nbsp &nbsp';
     }
   }
  echo ')</font><br><br><br>';
  }
}

$gr1='';
$gr2='';
for($t=0;$t<=$j;$t++)
 {
  if($min[$t]==0)
   {
     $gr1=$gr1.($t+1).' (';
     for($f=0;$f<=$i;$f++)
      {
      $gr1=$gr1.$e[$f][$t].' ';
      }
     if($t < $j-1)
     {
      $gr1=$gr1.')&nbsp &nbsp &nbsp &nbsp and &nbsp &nbsp &nbsp &nbsp';
     }
     else
      {
       $gr1=$gr1.')';
      }
   }
  if($min[$t]==1)
   {
     $gr2=$gr2.($t+1).' (';
     for($f=0;$f<=$i;$f++)
      {
       $gr2=$gr2.$e[$f][$t].' ';
      }
     if($t < $j-1)
      {
       $gr2=$gr2.')&nbsp &nbsp &nbsp &nbsp and &nbsp &nbsp &nbsp &nbsp';
      }
     else
      {
       $gr2=$gr2.')';
      }
    }
  }
echo 'Si on compl鑤era on obtiendra les m阭es centres donc on arr阾e avec un<br><font color=#2A8EE6 size="5.5"><b><u>Resultat Final:</u></b></font><br><br><font color="sky blue" size="5"><b><u>classs1:</u></b></font> '.$gr1.'<br><font color="sky blue" size="5"><b><u>classs2:</u></b></font> '.$gr2.'';


?>
</table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -