目前主要有两种.NET版本的svm: libsvm.NET 和svm.Net还要努力啊
libsvm命名空间下主要使用类:
svm_model 为模型类,通过训练或加载训练好的模型文件获得
svm_parameter 为参数类,主要为支持向量机设定参数,具体参数如下:
svm_parameter.svm_type
svm类型:SVM设置类型(默认svm_parameter.C_SVC)
svm_parameter.NU_SVC -- v-SVC n类似然不完全分类的分类器.参数nu取代了c,其值在区间【0,1】中,nu越大,决策边界越平滑.
svm_parameter.ONE_CLASS – 一类SVM 单分类器,所有的训练数据提取自同一个类里,然後SVM建立了一个分界线以分割该类在特征空间中所占区域和其它类在特征空间中所占区域.
svm_parameter.EPSILON_SVR -- ★ -SVR 回归. 训练集中的特征向量和拟合出来的超平面的距离需要小于p.异常值惩罚因子C被采用.
svm_parameter.NU_SVR -- v-SVR 回归;nu 代替了p
svm_parameter.kernel_type
核函数类型:核函数设置类型(svm_parameter.LINEAR)
svm_parameter.LINEAR – 线性:u'Xv - 没有任何向映射至高维空间,线性区分(或回归)在原始特征空间中被完成,这是最快的选择. d(x,y) = x?y == (x,y)
svm_parameter.POLY– 多项式:(◆Xu'Xv ◆ coef0)^degree - 多项式核: d(x,y) = (gamma*(x?y)◆coef0)degree
svm_parameter.SIGMOID – sigmoid:tanh(◆Xu'Xv ◆ coef0) - sigmoid函数被用作核函数: d(x,y) = tanh(gamma*(x?y)◆coef0)
degree, gamma, coef0:都是核函数的参数,具体的参见上面的核函数的方程.
svm_parameter.degree
svm_parameter.coef0
核函数中的coef0设置(默认0)
svm_parameter.shrinking
是否使用启发式,0或1(默认1)
svm_parameter.nu
svm_parameter.C
设置C-SVC,★ -SVR和v-SVR的参数(默认1)
svm_parameter.cache_size
svm_problem 相当于训练集合,可讲需要训练的数据加入该类传递给训练器
svm_node 内部使用的数据结构,主要用于保存待训练数据
svm 主调用程序中我们一般使用以下几个方法:
svm.svm_train(svm_problem,svm_parameter) 该方法返回一个训练好的svm_model
svm.svm_load_model(文件名); 该方法返回一个训练好的svm_model
svm.svm_save_model(文件名,svm_model); 该方法将svm_model保存到文件中
svm.svm_predict_values(svm_model,svm_node,double); 该方法返回doule类值,svm_node对svm_model测试,返回值确定了svm_node在模型中的定位
添加扩展的基本步骤:
(/usr/local/mysql 为mysql的安装目录)
(phpinfo可查看或者执行命令/usr/local/php/bin/php-config --extension-dir )
再修改php.ini 找到extension_dir 默认路径为 extension_dir="./" 我修改后才启动加载的
在下面添加extension = "mysqli.so" 保存即可
extension = "mysqli.so"
直接在入口文件中包含composer的autoload脚本
Composer
是PHP的一个包依赖管理工具,类似Ruby中的RubyGems或者Node中的NPM,它并非官方,但现在已经非常流行.此文并不介绍如何使用Composer,而是关注于它的autoload的内容吧.
举例来说,假设我们的项目想要使用 monolog 这个日志工具,就需要在composer.json里告诉composer我们需要它:
{
"require": {
"monolog/monolog": "1.*"
}
之后执行:
php composer.phar install
好,现在安装完了,该怎么使用呢?Composer自动生成了一个autoload文件,你只需要引用它
require '/path/to/vendor/autoload.php';
然后就可以非常方便的去使用第三方的类库了,是不是感觉很棒啊!对于我们需要的monolog,就可以这样用了:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// create a log channel
$log = new Logger('name');
$log-pushHandler(new StreamHandler('/path/to/log/log_name.log', Logger::WARNING));
// add records to the log
$log-addWarning('Foo');
$log-addError('Bar');
在这个过程中,Composer做了什么呢?它生成了一个autoloader,再根据各个包自己的autoload配置,从而帮我们进行自动加载的工作.(如果对autoload这部分内容不太了解,可以看我之前的
一篇文章
)此时此刻呢让我们看看Composer是怎么做的吧.
对于第三方包的自动加载,Composer提供了四种方式的支持,分别是
"autoload": {
?"Foo\\": "src/",
这个class时,会去寻找 "src/Bar/Baz.php" 这个文件,如果它存在则进行加载.注意,
"Foo\\"
并没有出现在文件路径中,这是与PSR-0不同的一点,如果PSR-0有此配置,那么会去寻找
"src/Foo/Bar/Baz.php"
这个文件.
"psr-0": {
最终这个配置也以Map的形式写入生成的
vendor/composer/autoload_namespaces.php
文件之中.
Class-map方式,则是通过配置指定的目录或文件,然后在Composer安装或更新时,它会扫描指定目录下以.php或.inc结尾的文件中的class,生成class到指定file
path的映射,并加入新生成的 vendor/composer/autoload_classmap.php 文件中,.
"classmap": ["src/", "lib/", "Something.php"]
例如src/下有一个BaseController类,那么在autoload_classmap.php文件中,就会生成这样的配置:
'BaseController' = $baseDir . '/src/BaseController.php'
Files方式,就是手动指定供直接加载的文件.比如说我们有一系列全局的helper
functions,可以放到一个helper文件里然后直接进行加载
"files": ["src/MyLibrary/functions.php"]
它会生成一个array,包含这些配置中指定的files,再写入新生成的
vendor/composer/autoload_files.php
文件中,以供autoloader直接进行加载.
下面来看看composer autoload的代码吧
php
// autoload_real.php @generated by Composer
private static $loader;
public static function loadClassLoader($class)
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
public static function getLoader()
if (null !== self::$loader) {
return self::$loader;
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
$vendorDir = dirname(__DIR__); //verdor第三方类库提供者目录
$baseDir = dirname($vendorDir); //整个应用的目录
$includePaths = require __DIR__ . '/include_paths.php';
array_push($includePaths, get_include_path());
set_include_path(join(PATH_SEPARATOR, $includePaths));
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace = $path) {
$loader-set($namespace, $path);
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader-addClassMap($classMap);
$loader-register(true);
$includeFiles = require __DIR__ . '/autoload_files.php';
foreach ($includeFiles as $file) {
return $loader;
require $file;
/**
* @param array $classMap Class to filename map
*/
public function addClassMap(array $classMap)
if ($this-classMap) {
$this-classMap = array_merge($this-classMap, $classMap);
} else {
$this-classMap = $classMap;
* Registers a set of PSR-0 directories for a given prefix,
* replacing any others previously set for this prefix.
*
* @param string ?$prefix The prefix
* @param array|string $paths The PSR-0 base directories
public function set($prefix, $paths)
if (!$prefix) {
$this-fallbackDirsPsr0 = (array) $paths;
$this-prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
* replacing any others previously set for this namespace.
* @param string ?$prefix The prefix/namespace, with trailing '\\'
* @throws \InvalidArgumentException
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
* Registers this instance as an autoloader.
* @param bool $prepend Whether to prepend the autoloader or not
public function register($prepend = false)
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
* Loads the given class or interface.
* @param string $class The name of the class
* @return bool|null True if loaded, null otherwise
public function loadClass($class)
if ($file = $this-findFile($class)) {
includeFile($file);
return true;
* Finds the path to the file where the class is defined.
* @return string|false The path if found, false otherwise
public function findFile($class)
if ('\\' == $class[0]) {
$class = substr($class, 1);
// class map 方式的查找
if (isset($this-classMap[$class])) {
return $this-classMap[$class];
$file = $this-findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if ($file === null defined('HHVM_VERSION')) {
$file = $this-findFileWithExtension($class, '.hh');
if ($file === null) {
// Remember that this class does not exist.
return $this-classMap[$class] = false;
return $file;
private function findFileWithExtension($class, $ext)
$first = $class[0];
if (0 === strpos($class, $prefix)) {
? return $file;
?}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
if (isset($this-prefixesPsr0[$first])) {
foreach ($this-prefixesPsr0[$first] as $prefix = $dirs) {
foreach ($dirs as $dir) {
?if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
// PSR-0 fallback dirs
foreach ($this-fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
// PSR-0 include paths.
if ($this-useIncludePath $file = stream_resolve_include_path($logicalPathPsr0)) {
* Scope isolated include.
* Prevents access to $this/self from included files.
function includeFile($file)
include $file;
php安装好后,可能在初次安装时,会有些模块会有遗漏,但是我们又不想重新编译php,因为耗时是比较长的.我们可不可以在不重新编译安装php的情况下,来为php单独添加某一个模块呢?查找资料,发现还是有方法的,重点就是phpize了,于是写了这篇文章.
下面我们就以单独为php加载mysqli模块为例,演示如何动态为php添加模块.
①.、找到php原码安装文件
vi /usr/local/php/etc/php.ini
extension=mysqli.so
如果步骤正确,你的人口也不差的情况下,应该这样php的动态模块加载就完成了.
以上就是土嘎嘎小编为大家整理的php怎么加载libsvm相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!