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

📄 knn.php

📁 KNN clustering with php
💻 PHP
字号:
<html><body>
<?
$ts=0;
$t[$ts++]='cloudy';
$t[$ts++]='cloudy';
$t[$ts++]='cloudy';
$t[$ts++]='sunny';
$t[$ts++]='rainy';
$t[$ts++]='cloudy';
$t[$ts++]='sunny';
$hs=0;
$h[$hs++]='high';
$h[$hs++]='low';
$h[$hs++]='low';
$h[$hs++]='high';
$h[$hs++]='low';
$h[$hs++]='high';
$h[$hs++]='low';
$vs=0;
$v[$vs++]='yes';
$v[$vs++]='no';
$v[$vs++]='yes';
$v[$vs++]='yes';
$v[$vs++]='yes';
$v[$vs++]='no';
$v[$vs++]='no';
$bcs=0;
$bc[$bcs++]='yes';
$bc[$bcs++]='no';
$bc[$bcs++]='no';
$bc[$bcs++]='yes';
$bc[$bcs++]='no';
$bc[$bcs++]='yes';
$bc[$bcs++]='no';

//display datas in which we're going to work
echo '<font color="sky blue" size="5"><u><b> data on which we will apply KNN Clustering:</b></u><br><br></font><table border="1"><tr><td><b>Precedent Gatherings</b></td><td><b>weather</b></td><td><b>Humidity</b></td><td><b>Wind</b></td><td><b>Good Gathering</b></td></tr>';
for($i=0;$i<$ts;$i++)
 {
  echo '<tr><td>E'.($i+1).'<td>'.$t[$i].'</td><td>'.$h[$i].'</td><td>'.$v[$i].'</td><td>'.$bc[$i].'</td></tr>';
 }
echo '</table><font size="3" color="sky blue"><i><b>We will use euclidien distance</b></i></font>';

//Enter the gathering conditions
if (isset($_POST['weather'])=="" and $_POST['weather']=="" and $_POST['wind']=="")
 {
  echo '<form action="'. $_SERVER['PHP_SELF'].'" method="post"><font color="sky blue" size="4"><b><u>Choose your gathering conditions:</u></font><br><br>E:( <select name="weather">
  <option value="cloudy">cloudy</option><option value="rainy">rainy</option><option value="sunny">sunny</option></select>,&nbsp&nbsp&nbsp<select name="humidity"><option value="high">high</option><option value="low">low</option>
  </select>,&nbsp&nbsp&nbsp<select name="wind"><option value="yes">yes</option>
  <option value="no">no</option></select>)</b>&nbsp<br><br><b>Desired nearest records number </b><select name="rn">';
  for($i=1;$i<$ts+1;$i++)
   {
    echo '<option value="'.$i.'">'.$i.'</option>';
   }
  echo '</select><br><br><b> Vote type </b><select name="vote"><option value="m">Unbalanced</option><option value="mp">Balanced</option></select><br><br> &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
  &nbsp &nbsp &nbsp<button type="submit">Go</button><br>';
 }

else
 {
    $q1 = $_POST['weather'];
    $q2 = $_POST['humidity'];
    $q3 = $_POST['wind'];
    $q4 = $_POST['rn'];
    $q5 = $_POST['vote'];
    echo '<br><br><font color="sky blue" size=4><b><u>Query entered E: </u></b></font> <font color="#41D3DD" size="3"><b>weather: </b>'.$q1.'<b> humidity: </b>'.$q2.'<b> wind: </b>'.$q3.'<b> nearest records number: </b>'.$q4;
    if($q5=='m')
     {
      echo '<b> vote type: </b>normal (unpondered)</font>';
     }
    else
     {
     echo '<font color="#41D3DD"><b>pondered (balanced): </b>';
     }
    for($i=0;$i<$ts;$i++)
     {
      $d[$i]=0;
      if($t[$i]!=$q1)
       {
        $d[$i]++;
       }
      if($h[$i]!=$q2)
       {
        $d[$i]++;
       }
      if($v[$i]!=$q3)
       {
        $d[$i]++;
       }
      $d[$i]=sqrt($d[$i]); 
     }

    //show distances between entered and existant gatherings
    echo '<br><br><font size="5" color="sky blue"><b><u>Distances table:</u></b></font><br><br><table border="1"><tr>';
    for($i=0;$i<$ts;$i++)
     {
      echo '<td>d(E,E'.($i+1).')</td>';
     }
    echo '</tr><tr>';
    for($i=0;$i<$ts;$i++)
     {
      echo '<td>'.$d[$i].'</td>';
     }
    echo '</table></tr></table>';

    $c=array();
    for($i=0;$i<$ts;$i++)
     {
      array_push ($c, $d[$i]);
     }

    //search number of positif or negatif gathering looking to the number of records to take in consideration and to the vote type
    asort ($c);
    reset ($c);
    $k=array();
    $v=array();
    $vo=0;
    $vn=0;
    $vpo=0;
    $vpn=0;
    for($j=0;$j<$q4;$j++)
     {
      list($l,$m)=each($c);
      $k[$j]=$l;
      $v[$j]=$m;
      if($bc[$l]=='yes')
       {
        $vo++;
        if($m !=0)
         {
          $vpo=$vpo+(1/$m);
         }
        else
         {
          $vpo=0;
         }
       }
      else
       {
        $vn++;
        if($m !=0)
         {
          $vpn=$vpn+(1/$m);
         }
        else
         {
          $vpn=0;
         }
       }
     }

    //return result of all calculations
    if($q5=='m')
     {
      echo '<b>Yes</b> vote = '.$vo.' <b>No</b> vote = '.$vn.' so the final result is ';
      if($vo>$vn)
       {
        echo '<font color="olive" size="4"><b>Yes you can gather mushrooms</b>.';
       }
      else
       {
        echo '<font color="olive" size="4">No you can not gather mushrooms</b>.';
       }
     }
    else
     {
      echo '<br><b>Yes</b> Vote balanved = '.$vpo.' while <b>NO</b> pondered vote  = '.$vpn.' so the final result is ';
      if($vpo>$vpn)
       {
        echo '<font color="olive" size="4"><b>Yes you can gather mushrooms</b>.';
       }
      else
       {
        echo '<font color="olive" size="4">No you can not gather mushrooms</b>.';
       }
     }
 }

?>
</body></html>

⌨️ 快捷键说明

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