0

我从中得到 knn 算法的代码http://jameshamilton.eu/old/datamining/k-nearest_neighbour.php?source。我在未定义的函数行中遇到错误。请帮助我消除此错误。这是 k-最近算法的 akgo

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="description" content="K Nearest Neighbour Algorithm - Data Mining" />
<meta name="keywords" content="k nearest neighbour, data mining, php, computer science, james hamilton" />
<meta http-equiv="Content-Language" content="en-gb" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="robots" content="all" />
<meta name="revisit-after" content="1 days" />

<title>K Nearest Neighbour Algorithm - Data Mining</title>
<?php
include('admin_nb.php');
 // $date=$_GET['id'];echo"</br>";
  //$d=$_GET['id1'];

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db=mysql_select_db("vas1",$con);


?>

</head>

<body>

<?php
if(isset($_GET['source'])) {
    highlight_file(__FILE__);
}else{
if(!isset($_POST['k'])) $_POST['k'] = 3;
?>


    <form method="post" action="<?=$_SERVER['PHP_SELF']?>">
    K:&nbsp;
    <select name="k">
        <?php foreach(range(1, 10) as $k) { ?>
            <option value="<?=$k?>" <?php if($k == $_POST['k']) { ?> selected="selected"<?php } ?>><?=$k?></option>
        <?php } ?>
    </select>


    <table id="patients" cellpadding="3" cellspacing="0">
        <tr>

  <th>highest_dialled_call_type</th>
   <th>Age_less_than_30</th>
   <th>Age_greater_than_30</th>
   <th>Payment_on_time</th>
   <th>Pkg_activation</th>
   <th>Customer_profile</th>
   <th>Churn</th>
   <th>Proposed_pkg</th>
   <th>Distance</th>


        </tr>

    <?php

        /*
            --
            -- Table structure for table `datamining_diagnoses`
            --

            CREATE TABLE IF NOT EXISTS `datamining_diagnoses` (
              `id` int(11) NOT NULL auto_increment,
              `sorethroat` tinyint(4) NOT NULL default '0',
              `fever` tinyint(4) NOT NULL default '0',
              `swollenglands` tinyint(4) NOT NULL default '0',
              `congestion` tinyint(4) NOT NULL default '0',
              `headache` tinyint(4) NOT NULL default '0',
              `diagnosis` varchar(50) NOT NULL default '',
              PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

            --
            -- Dumping data for table `datamining_diagnoses`
            --

            INSERT INTO `datamining_diagnoses` (`id`, `sorethroat`, `fever`, `swollenglands`, `congestion`, `headache`, `diagnosis`) VALUES
            (1, 1, 1, 1, 1, 1, 'Strepthroat'),
            (2, 0, 0, 0, 1, 1, 'Allergy'),
            (3, 1, 1, 0, 1, 0, 'Cold'),
            (4, 1, 0, 1, 0, 0, 'Strepthroat'),
            (5, 0, 1, 0, 1, 0, 'Cold'),
            (6, 0, 0, 0, 1, 0, 'Allergy'),
            (7, 0, 0, 1, 0, 0, 'Strepthroat'),
            (8, 1, 0, 0, 1, 1, 'Allergy'),
            (9, 0, 1, 0, 1, 1, 'Cold'),
            (10, 1, 1, 0, 1, 1, 'Cold');
        */

        /*
            function getRows takes an SQL query string (and a optional array key type - NUM or ASSOC)
            and returns and array containing all the rows of the query.

                define(ASSOC, 1);
                define(NUM, 2);

                function getRows($sql, $type=ASSOC) {
                    $result = mysql_query($sql);
                    if($type == ASSOC) {
                        while($row = mysql_fetch_array($result))
                            $ret[] = $row;
                    }else if($type == NUM) {
                        while($row = mysql_fetch_row($result))
                            $ret[] = $row;
                    }else{
                        die("type should be NUM or ASSOC");
                    }
                    return $ret;
                }
        */


        $rows =getRows("SELECT * FROM knowledge_base;", NUM);


        $closest = -1;

        if(isset($_POST['diagnose'])) {
            $distance = array_fill(0, count($rows), 0);

            $instance = $_POST['instance'];

            for($i = 0; $i < count($rows); $i++) {

                for($j = 1; $j < 8; $j++) {
                    if($rows[$i][$j] != $instance[$j])
                        $distance[$i]++;
                }

            }


            asort($distance);

            $kclosest = array();

            foreach($distance as $key => $d)
                if(++$x <= $_POST[k]) {
                     $kclosest[$rows[$key][8]]++;
                     $newdistance[$key] = $d;
                }

            arsort($kclosest);
            $diagnosis = key($kclosest);

       }

        $a = 0;
        foreach($rows as $row) {

        ?>
            <tr <?php if(isset($newdistance[$a])) { ?> class="selected"<?php } ?>>
                 <td><?=$row[0] == international ? "international" : "onnet" : "offnet"; ?></td>
                <td><?=$row[1] == no ? "no" : "yes"; ?></td>
                <td><?=$row[2] == no ? "no" : "yes"; ?></td>
                <td><?=$row[3] == no ? "no" : "yes"; ?></td>
                <td><?=$row[4] == no ? "no" : "yes"; ?></td>
      <td><?=$row[5] == no ? "new" : "loyal"; ?></td>
                <td><?=$row[6] == no ? "no" : "yes"; ?></td>
                <td><?=$row[7]?></td>
                <td><?=$distance[$a++]?></td>
            </tr>

        <?php

        }
        ?>
        <tr>

                <td><input type="hidden" name="instance[]" value="11" />11</td>
                <td><select name="instance[]">
                        <option value="0"<?php if($_POST['instance'][0] == "international") echo "selected=\"selected\"";?>>international</option>
                        <option value="1"<?php if($_POST['instance'][0] == "onnet") echo "selected=\"selected\"";?>>onnet</option>
      <option value="2"<?php if($_POST['instance'][0] == "offnet") echo "selected=\"selected\"";?>>offnet</option>
                    </select>
                </td>
                <td><select name="instance[]">
                        <option value="0"<?php if($_POST['instance'][1] == "0") echo "selected=\"selected\"";?>>no</option>
                        <option value="1"<?php if($_POST['instance'][1] == "1") echo "selected=\"selected\"";?>>yes</option>
                    </select>
                </td>
                <td><select name="instance[]"&gt.

错误出现在行:$rows =getRows("SELECT * FROM knowledge_base;", NUM); 错误是: Fatal error: Call to undefined function getRows() in C:\wamp\www\vas1\knn1.php on line 122 请帮助消除此错误

4

2 回答 2

1

您已经注释掉了 getRows() 函数的定义。

于 2013-06-26T16:37:03.780 回答
0

取消注释您的 getRows() 函数,使其看起来像:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="description" content="K Nearest Neighbour Algorithm - Data Mining" />
<meta name="keywords" content="k nearest neighbour, data mining, php, computer science, james hamilton" />
<meta http-equiv="Content-Language" content="en-gb" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="robots" content="all" />
<meta name="revisit-after" content="1 days" />

<title>K Nearest Neighbour Algorithm - Data Mining</title>
<?php
include('admin_nb.php');
 // $date=$_GET['id'];echo"</br>";
  //$d=$_GET['id1'];

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db=mysql_select_db("vas1",$con);


?>

</head>

<body>

<?php
if(isset($_GET['source'])) {
    highlight_file(__FILE__);
}else{
if(!isset($_POST['k'])) $_POST['k'] = 3;
?>


    <form method="post" action="<?=$_SERVER['PHP_SELF']?>">
    K:&nbsp;
    <select name="k">
        <?php foreach(range(1, 10) as $k) { ?>
            <option value="<?=$k?>" <?php if($k == $_POST['k']) { ?> selected="selected"<?php } ?>><?=$k?></option>
        <?php } ?>
    </select>


    <table id="patients" cellpadding="3" cellspacing="0">
        <tr>

  <th>highest_dialled_call_type</th>
   <th>Age_less_than_30</th>
   <th>Age_greater_than_30</th>
   <th>Payment_on_time</th>
   <th>Pkg_activation</th>
   <th>Customer_profile</th>
   <th>Churn</th>
   <th>Proposed_pkg</th>
   <th>Distance</th>


        </tr>

    <?php
                define(ASSOC, 1);
                define(NUM, 2);

                function getRows($sql, $type=ASSOC) {
                    $result = mysql_query($sql);
                    if($type == ASSOC) {
                        while($row = mysql_fetch_array($result))
                            $ret[] = $row;
                    }else if($type == NUM) {
                        while($row = mysql_fetch_row($result))
                            $ret[] = $row;
                    }else{
                        die("type should be NUM or ASSOC");
                    }
                    return $ret;
                }



        $rows =getRows("SELECT * FROM knowledge_base;", NUM);


        $closest = -1;

        if(isset($_POST['diagnose'])) {
            $distance = array_fill(0, count($rows), 0);

            $instance = $_POST['instance'];

            for($i = 0; $i < count($rows); $i++) {

                for($j = 1; $j < 8; $j++) {
                    if($rows[$i][$j] != $instance[$j])
                        $distance[$i]++;
                }

            }


            asort($distance);

            $kclosest = array();

            foreach($distance as $key => $d)
                if(++$x <= $_POST[k]) {
                     $kclosest[$rows[$key][8]]++;
                     $newdistance[$key] = $d;
                }

            arsort($kclosest);
            $diagnosis = key($kclosest);

       }

        $a = 0;
        foreach($rows as $row) {

        ?>
            <tr <?php if(isset($newdistance[$a])) { ?> class="selected"<?php } ?>>
                 <td><?=$row[0] == international ? "international" : "onnet" : "offnet"; ?></td>
                <td><?=$row[1] == no ? "no" : "yes"; ?></td>
                <td><?=$row[2] == no ? "no" : "yes"; ?></td>
                <td><?=$row[3] == no ? "no" : "yes"; ?></td>
                <td><?=$row[4] == no ? "no" : "yes"; ?></td>
      <td><?=$row[5] == no ? "new" : "loyal"; ?></td>
                <td><?=$row[6] == no ? "no" : "yes"; ?></td>
                <td><?=$row[7]?></td>
                <td><?=$distance[$a++]?></td>
            </tr>

        <?php

        }
        ?>
        <tr>

                <td><input type="hidden" name="instance[]" value="11" />11</td>
                <td><select name="instance[]">
                        <option value="0"<?php if($_POST['instance'][0] == "international") echo "selected=\"selected\"";?>>international</option>
                        <option value="1"<?php if($_POST['instance'][0] == "onnet") echo "selected=\"selected\"";?>>onnet</option>
      <option value="2"<?php if($_POST['instance'][0] == "offnet") echo "selected=\"selected\"";?>>offnet</option>
                    </select>
                </td>
                <td><select name="instance[]">
                        <option value="0"<?php if($_POST['instance'][1] == "0") echo "selected=\"selected\"";?>>no</option>
                        <option value="1"<?php if($_POST['instance'][1] == "1") echo "selected=\"selected\"";?>>yes</option>
                    </select>
                </td>
                <td><select name="instance[]"&gt.
于 2013-06-26T16:38:41.357 回答