资源简介
SVM算法的代码,用pyhon实现的,可直接用,很方便,下载即可用!该算法可用于机器学习分类研究,是一种典型的分类算法,非常适合论文实验。
代码片段和文件信息
#!/usr/bin/env python
from __future__ import print_function
import sys
try:
import scipy
from scipy import sparse
except:
scipy = None
sparse = None
__all__ = [‘svm_read_problem‘ ‘evaluations‘ ‘csr_find_scale_param‘ ‘csr_scale‘]
def svm_read_problem(data_file_name return_scipy=False):
“““
svm_read_problem(data_file_name return_scipy=False) -> [y x] y: list x: list of dictionary
svm_read_problem(data_file_name return_scipy=True) -> [y x] y: ndarray x: csr_matrix
Read LIBSVM-format data from data_file_name and return labels y
and data instances x.
“““
prob_y = []
prob_x = []
row_ptr = [0]
col_idx = []
for i line in enumerate(open(data_file_name)):
line = line.split(None 1)
# In case an instance with all zero features
if len(line) == 1: line += [‘‘]
label features = line
prob_y += [float(label)]
if scipy != None and return_scipy:
nz = 0
for e in features.split():
ind val = e.split(“:“)
val = float(val)
if val != 0:
col_idx += [int(ind)-1]
prob_x += [val]
nz += 1
row_ptr += [row_ptr[-1]+nz]
else:
xi = {}
for e in features.split():
ind val = e.split(“:“)
xi[int(ind)] = float(val)
prob_x += [xi]
if scipy != None and return_scipy:
prob_y = scipy.array(prob_y)
prob_x = scipy.array(prob_x)
col_idx = scipy.array(col_idx)
row_ptr = scipy.array(row_ptr)
prob_x = sparse.csr_matrix((prob_x col_idx row_ptr))
return (prob_y prob_x)
def evaluations_scipy(ty pv):
“““
evaluations_scipy(ty pv) -> (ACC MSE SCC)
ty pv: ndarray
Calculate accuracy mean squared error and squared correlation coefficient
using the true values (ty) and predicted values (pv).
“““
if not (scipy != None and isinstance(ty scipy.ndarray) and isinstance(pv scipy.ndarray)):
raise TypeError(“type of ty and pv must be ndarray“)
if len(ty) != len(pv):
raise ValueError(“len(ty) must be equal to len(pv)“)
ACC = 100.0*(ty == pv).mean()
MSE = ((ty - pv)**2).mean()
l = len(ty)
sumv = pv.sum()
sumy = ty.sum()
sumvy = (pv*ty).sum()
sumvv = (pv*pv).sum()
sumyy = (ty*ty).sum()
with scipy.errstate(all = ‘raise‘):
try:
SCC = ((l*sumvy-sumv*sumy)*(l*sumvy-sumv*sumy))/((l*sumvv-sumv*sumv)*(l*sumyy-sumy*sumy))
except:
SCC = float(‘nan‘)
return (float(ACC) float(MSE) float(SCC))
def evaluations(ty pv useScipy = True):
“““
evaluations(ty pv useScipy) -> (ACC MSE SCC)
ty pv: list tuple or ndarray
useScipy: convert ty pv to ndarray and use scipy functions for the evaluation
Calculate accuracy mean squared error and squared correlation coefficient
using the true values (ty) and predicted values (pv).
“““
if scipy != None and useScipy:
return evaluations_scipy(scipy.asarray(ty) scipy.asarray(pv))
if len(ty) != len(pv):
raise ValueError(“len(ty) must be equal to len(pv)“)
total_correct = total_error = 0
sumv = sumy = sumvv = sumyy = sumvy = 0
for v y in zip(pv ty):
if y == v:
total_correct += 1
total_error += (v-y)*(v-y)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5121 2018-07-15 22:16 python\commonutil.py
文件 32 2018-07-15 22:16 python\Makefile
文件 15822 2018-07-15 22:16 python\README
文件 13631 2018-07-15 22:16 python\svm.py
文件 9395 2018-07-15 22:16 python\svmutil.py
相关资源
- 基于PSO的SVM参数寻优代码
- 葡萄酒数据集
- opencv 人数统计
- 基于PSO的LS-SVM特征选择与参数优化算
- LS-SVM,预测时间序列
- libsvm-3.20版本 64位系统可用,内涵测试
- 支持向量机简介ppt
- 异常检测高斯分布模型+测试数据
- coursera吴恩达机器学习讲义pdf
- coursera吴恩达机器学习全套教学视频
- 红酒、白酒质量数据集,可作为机器
- LogiReg_data.txt 逻辑回归 成绩与录取
- 鸢尾花(iris)数据集209307
- hog+lbp+svm人脸识别 特征融合
- 机器学习文献综述
- 机器学习西瓜数据集4.0
- SVM支持向量机分类鸢尾花数据集iris及
- 鸢尾花数据集xlsx格式
- BP神经网络详解
- 研究生院凸优化授课视频(重发)
- 《集体智慧编程》(Programming Collect
- 用FormatDataLibsvm.xls将数据转换成Libsv
- KNN识别LED数码管
- LS_SVMlib支持向量机安装包
- machine learning SVM classify algorithm
- 支持向量机libsvm-2.88(最新版
- ABC-SVM算法,逐行代码注释简单易懂,
- 利用OpenCV,通过SVM识别图像中图形链
- libsvm工具包含网格法查找最优解函数
- libsvm工具包含网格法查找最优解函数
评论
共有 条评论