PHP 8.3.4 Released!

Exemples

Le processus basique consiste à définir des paramètres, soumettre des données d’entraînement pour la génération d'un modèle, puis, faire des prédictions basés sur ce modèle. Il y a un jeu par défaut de paramètres qui doivent donner des résultats avec la plupart des entrées, aussi, nous allons commencer par regarder du côté de ces données.

Les données sont soumises via un fichier, un flux ou un tableau. Si elles sont fournies via un fichier ou un flux, elles doivent contenir une ligne par exemple d’entraînement, ligne devant être formatée sous la forme d'une classe entière (habituellement 1 et -1), suivi par une séries de paires clés/fonctionnalités, dans un ordre croissant des fonctionnalités. Les fonctionnalités sont des entiers, et les valeurs, des nombres à virgules flottantes dans l'intervalle 0-1. Par exemple :

-1 1:0.43 3:0.12 9284:0.2

Dans un problème de classification de document, par exemple concernant le spam, chaque ligne doit représenter un document. Il doit y avoir 2 classes, -1 pour les spam, 1 pour les ham. Chaque fonctionnalité représente des mots, et les valeurs représentent l'importance de ces mots dans le document (par exemple le fréquence de rencontre de ces mots dans le document, avec le total dans le bon intervalle). Les fonctionnalités présentant la valeur 0 (i.e. le mot n'apparaît pas du tout dans le document) ne seront tout simplement pas inclues.

Dans mode tableau, les données doivent être passées sous la forme de tableaux de tableaux. Chaque sous-tableau doit avoir la classe comme premier élément, plus, clés => jeux de valeurs pour les paires de fonctionnalité/valeur.

Ces donnée sont passées à la fonction d’entraînement de la classe SVM, qui retournera un modèle SVM en cas de succès.

Une fois le modèle généré, il peut être utilisé pour faire des prédictions sur les données précédentes non vues. Elles peuvent être passées sous forme de tableau à la fonction de prédiction du modèle, dans le même format que précédemment, mais sans le libellé. La réponse sera la classe.

Les modèles peuvent être sauvegardés et restaurés à la demande, en utilisant les fonctions de sauvegarde et de chargement, qui prennent toutes les 2 comme argument le chemin vers le fichier correspondant.

Exemple #1 Entraînement depuis un tableau

<?php
$data
= array(
array(-
1, 1 => 0.43, 3 => 0.12, 9284 => 0.2),
array(
1, 1 => 0.22, 5 => 0.01, 94 => 0.11),
);

$svm = new SVM();
$model = $svm->train($data);

$data = array(1 => 0.43, 3 => 0.12, 9284 => 0.2);
$result = $model->predict($data);
var_dump($result);
$model->save('model.svm');
?>

Résultat de l'exemple ci-dessus est similaire à :

int(-1)

Exemple #2 Entraînement depuis un fichier

<?php
$svm
= new SVM();
$model = $svm->train("traindata.txt");
?>

add a note

User Contributed Notes 6 notes

up
0
razvan_bc at yahoo dot com
1 year ago
the example rated negative rated by the guy sign "6765419 at qq dot com" also works too!

<?php
$data
= array(
array(-
1, 1 =>170, 2 => 60),//-1 表示男生,key 1表示身高,key 2表示体重=Represents a boy, key 1 represents height, key 2 represents weight
array(-1, 1 =>180, 2 => 70),
array(
1, 1 => 160, 2 => 46),//1 表示女生,key 1表示身高,key 2表示体重=Represents a girl, key 1 represents height, key 2 represents weight
array(1, 1 => 155, 2 => 40),
);
$svm = new SVM();
$model = $svm->train($data);
$data = array( 1 => 165, 2 =>60);//测试数据 =Test Data
$result = $model->predict($data);
echo
var_dump($result);//echo var_export($result);
//return;
?>

so i got :

float(-1)
up
0
razvan_bc at yahoo dot com
1 year ago
ok i did more tests..

getting the source
https://github.com/ianbarber/php-svm/blob/master/tests/002_predict.phpt modified ..
<?php
$svm
= new svmmodel();
//$result = $svm->load(dirname(__FILE__) . '/australian.model');
$result = $svm->load('australian.model');

if(
$result) {
$data = array(
"1" => 1,
2 => -0.731729,
3 => -0.886786,
4 => -1,
5 => 0.230769,
"6" => -0.25,
7 => -0.783509,
8 => 1,
9 => 1,
10 => "-0.820896",
11 => -1,
13 => -0.92,
"14" => "-1"
);
$result = $svm->predict($data);
if(
$result > 0) {
echo
"ok";
print_r($result);
} else {
echo
"predict failed: $result";
}
} else {
echo
"loading failed";
}
?>

with additional https://github.com/ianbarber/php-svm/blob/master/tests/australian.scale dropped inside the test folder where .php file is located i am able after running to get the result:
================================

ok1

so it's work
up
0
razvan_bc at yahoo dot com
1 year ago
i forgot a detail!
the installation folders if you think to install it manually in windows xampp should be c:\xampp\php\lib\libsvm-3.1 (for the files i described in the first post) and extension in c:\xampp\php\ext (php_svm.dll)

works.good luck
up
0
razvan_bc at yahoo dot com
1 year ago
from pecl.php.net i download svm php_svm-0.2.3-8.1-ts-vs16-x64.zip so i read in README.md ..

=====================================================
Data is supplied in either a file, a stream, or as an 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:

-1 1:0.43 3:0.12 9284:0.2

=====================================================

so creating traindata.txt with the content -1 1:0.43 3:0.12 9284:0.2 leads me to use it in the second example:

<?php
$svm
= new SVM();
$model = $svm->train("traindata.txt");
$model->save('model2.svm');
?>

and running and editing the model2.svm i got the content:
-------------------------------------------------------------------
svm_type c_svc
kernel_type rbf
gamma 0.00010771219302024989
nr_class 1
total_sv 0
rho
label -1
nr_sv 0
SV
--------------------------------------------------------------------

so yes i think it's work, how i said i need to do more tests to get control with main functions to think to other more complicated
up
0
razvan_bc at yahoo dot com
1 year ago
premises:php 8.1 ,windows 64
----------------------------------

install (for beginners)
--------
after i visit https://github.com/ianbarber/php-svm

and i got from url found on page(install script)
.. http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm+tar.gz

and manual install it:
1.php.ini
(after the main group extension=... about 12 pieces)
...
extension=svm
...

2.I put manually inside php a folder called libsvm-3.1 then i unzip there libsvm.dll , libsvmread.mexw64 ,libsvmwrite.mexw64 , svmpredict.mexw64 , svm-predict.exe, svm-scale.exe , svm-toy.exe , svmtrain.mexw64, svm-train.exe !

running
<?php

$data
= array(
array(-
1, 1 => 0.43, 3 => 0.12, 9284 => 0.2),
array(
1, 1 => 0.22, 5 => 0.01, 94 => 0.11),
);

$svm = new SVM();
$model = $svm->train($data);

$data = array(1 => 0.43, 3 => 0.12, 9284 => 0.2);
$result = $model->predict($data);
var_dump($result);
$model->save('model.svm');

?>

via server(apache ,php,mariadb;even custom or xampp) now i got results:
i got model.svm with the content
================================
svm_type c_svc
kernel_type rbf
gamma 0.00010771219302024989
nr_class 2
total_sv 2
rho 0
label 1 -1
nr_sv 1 1
SV
1 1:0.22 5:0.01 94:0.11
-1 1:0.43 3:0.12 9284:0.2
=================================

so i think is very cool ..for a startup.
i will look around phpt files from github to understand why in yesterday's tests i got errors with some function witch require 2 parameters and not one like in the manual
up
-8
6765419 at qq dot com
10 years ago
$data = array(
array(-1, 1 =>170, 2 => 60),//-1表示男生,key 1表示身高,key 2表示体重
array(-1, 1 =>180, 2 => 70),
array(1, 1 => 160, 2 => 46),//1表示女生,key 1表示身高,key 2表示体重
array(1, 1 => 155, 2 => 40),
);
$svm = new SVM();
$model = $svm->train($data);
$data = array( 1 => 165, 2 =>60);//测试数据
$result = $model->predict($data);
echo var_export($result);
return;
To Top