Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 12076

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Examples</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="svm.resources.html">Resource Types</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="class.svm.html">SVM</a></div>
 <div class="up"><a href="book.svm.html">SVM</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="svm.examples" class="chapter">
 <h1>Examples</h1>


 <p class="para">
  The basic process is to define parameters, supply training data to generate a 
  model on, then make predictions based on the model. There are a default set 
  of parameters that should get some results with most any input, so we&#039;ll start 
  by looking at the data. 
 </p>
 <p class="para">
  Data is supplied in either a file, a stream, or as an array. If supplied in 
  a file or a stream, it must contain one line per training example, which must 
  be formatted as an integer class (usually 1 and -1) followed by a series of 
  feature/value pairs, in increasing feature order. The features are integers, 
  the values floats, usually scaled 0-1. For example:
 </p>
 <p class="para">
  -1 1:0.43 3:0.12 9284:0.2
 </p>
 <p class="para">
  In a document classification problem, say a spam checker, each line would 
  represent a document. There would be two classes, -1 for spam, 1 for ham. 
  Each feature would represent some word, and the value would represent that 
  importance of that word to the document (perhaps the frequency count, with 
  the total scaled to unit length). Features that were 0 (e.g. the word did 
  not appear in the document at all) would simply not be included.
 </p>
 <p class="para">
  In array mode, the data must be passed as an array of arrays. Each sub-array 
  must have the class as the first element, then key =&gt; value sets for the 
  feature values pairs.
 </p>
 <p class="para">
  This data is passed to the SVM class&#039;s train function, which will return an 
  SVM model is successful.
 </p>
 <p class="para">
  Once a model has been generated, it can be used to make predictions about 
  previously unseen data. This can be passed as an array to the model&#039;s 
  predict function, in the same format as before, but without the label. 
  The response will be the class. 
 </p>
 <p class="para">
  Models can be saved and restored as required, using the save and load 
  functions, which both take a file location. 
 </p>
 <p class="para">
  <div class="example" id="example-4654">
   <p><strong>Example #1 Train from array</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.43</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.12</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">9284&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.2</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.22</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.01</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">94&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.11</span><span style="color: #007700">),<br />);<br /><br /></span><span style="color: #0000BB">$svm&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SVM</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$model&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$svm</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">train</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.43</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.12</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">9284&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$model</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">predict</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$model</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">save</span><span style="color: #007700">(</span><span style="color: #DD0000">'model.svm'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>The above example will output
something similar to:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(-1)
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4655">
   <p><strong>Example #2 Train from a file</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$svm&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SVM</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$model&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$svm</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">train</span><span style="color: #007700">(</span><span style="color: #DD0000">"traindata.txt"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="svm.resources.html">Resource Types</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="class.svm.html">SVM</a></div>
 <div class="up"><a href="book.svm.html">SVM</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>