Wednesday, 13 November 2019

Complete Prediction code

Hi All
Follow these four steps
DB Name :predictdb
Table Name  : predictdata



CREATE TABLE IF NOT EXISTS `predictdata` (
  `pid` int(11) NOT NULL,
  `temperature` text NOT NULL,
  `pressure` text NOT NULL,
  `sugar` text NOT NULL,
  `totalcount` text NOT NULL,
  `inputstring` text NOT NULL,
  `predictionresult` text NOT NULL
)


1. Input form and DB save with input string generation
----------------------------------------------------------------------------------------------------------

<html>
<body>
<form method=post >
<pre>
Patient Id  <input name=pid>
Temperature <select name=temperature>
            <option value='vhigh'>Very High</option>
            <option value='high'>High</option>
            <option value='medium'>Medium</option>
            <option value='low'>Low</option>
            <option value='vlow'>Very Low</option>
            </select>
Pressure <select name=pressure>
            <option value='vhigh'>Very High</option>
            <option value='high'>High</option>
            <option value='medium'>Medium</option>
            <option value='low'>Low</option>
            <option value='vlow'>Very Low</option>
            </select>
Sugar    <select name=sugar>
            <option value='vhigh'>Very High</option>
            <option value='high'>High</option>
            <option value='medium'>Medium</option>
            <option value='low'>Low</option>
            <option value='vlow'>Very Low</option>
            </select>
Totalcount <input name=count>
<input type=submit name='btnsave' value='Save'>
</form>
<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("predictdb",$con);

if(isset($_POST['btnsave']))
{
$totalcount='';
if($_POST['count']<=100)
$totalcount='low';
else
$totalcount='high';
$inputstring="t_".$_POST['temperature']." p_".$_POST['pressure']." s_".$_POST['sugar']." tc_".$totalcount;
$qr="insert into predictdata values('$_POST[pid]','$_POST[temperature]','$_POST[pressure]','$_POST[sugar]','$totalcount','$inputstring','')";
echo $qr;
mysql_query($qr,$con);

}


?>

</body>
</html>


----------------------------------------------------------------------------------------------------------

2. Download testdata.csv
----------------------------------------------------------------------------------------------------------


<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("predictdb",$con);
header('Content-Type: text/csv; charset=utf-8');
      header('Content-Disposition: attachment; filename=da.csv');
       $output = fopen("php://output", "w");
     fputcsv($output, array('pid','inputstring'));
         $query = "select *  from predictdata";
         $result = mysql_query($query, $con);
         while($row = mysql_fetch_array($result))
         {
              fputcsv($output,array($row['pid'],$row['inputstring']));
             
         }
      fclose($output);

?>
----------------------------------------------------------------------------------------------------------


3. Predict using python
----------------------------------------------------------------------------------------------------------
from nltk import NaiveBayesClassifier as nbc
from nltk.tokenize import word_tokenize
from itertools import chain
import csv

with open('trainingdata.csv','r'as csvinput:
    reader=csv.reader(csvinput,delimiter=",")
    rownum = 0 
    training_data = []

    for row in reader:
        training_data.append (row)
        rownum += 1

vocabulary = set(chain(*[word_tokenize(i[0].lower()) for i in training_data]))

feature_set = [({i:(i in word_tokenize(sentence.lower())) for i in vocabulary},tag) for sentence, tag in training_data]

classifier = nbc.train(feature_set)

with open('testdata.csv','r'as csvinput:
    with open('data.csv''w'as csvoutput:
        writer = csv.writer(csvoutput, lineterminator='\n')
        reader1 = csv.reader(csvinput)

        all = []
        row = next(reader1)
        

        for row in reader1:
            test_sentence = row[1]
            featurized_test_sentence =  {i:(i in word_tokenize(test_sentence.lower())) for i in vocabulary}
            print ("test_sent:",test_sentence)
            print ("tag:",classifier.classify(featurized_test_sentence))
            row.append(classifier.classify(featurized_test_sentence))
            all.append(row)
        writer.writerows(all)


----------------------------------------------------------------------------------------------------------


4. Upload result to web using data.csv
----------------------------------------------------------------------------------------------------------
<form method='POST' enctype='multipart/form-data'>
<center>
Upload CSV FILE: <input type='file' name='csv_info' />
<input type='submit' name='submit' value='Upload' style="height: 50px; width: 150px; left: 250; top: 250;"/>
</center>
</form>
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("predictdb",$con);
if(isset($_POST['submit'])){
if($_FILES['csv_info']['name']){
  $arrFileName = explode('.',$_FILES['csv_info']['name']);
   if($arrFileName[1] == 'csv'){
     $handle = fopen($_FILES['csv_info']['tmp_name'], "r");
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
         $id=$data[0];
         $result = $data[2];
     
         $sql="update predictdata set predictionresult='$result' where pid='$id'";
mysql_query($sql);
                                   }
                        fclose($handle);
}
}
}
?>


----------------------------------------------------------------------------------------------------------