机器学习--------SVM
#SVM的使用
(结合具体代码说明,代码参考邹博老师的代码)
1、使用numpy中的loadtxt读入数据文件
data:鸢尾花数据
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
读取:
:path路径
:dtype读取类型
:delimiter分隔符
:converters- A dictionary mapping column number to a function that will parse the column string into the desired value. E.g., if column 0 is a date string: ``converters = {0: datestr2num}``. Converters can also be used to provide a default value for missing data (but see also genfromtxt): ``converters = {3: lambda s: float(s.strip() or 0)}``.
:Default None.
*data
[[5.1, 3.5, 1.4, 0.2, 0. ], [4.9, 3. , 1.4, 0.2, 0. ], [4.7, 3.2, 1.3, 0.2, 0. ], [4.6, 3.1, 1.5, 0.2, 0. ],[5. , 3.6, 1.4, 0.2, 0. ]]
2、数据分训练测试集
*split用法
def split(ary,indices_or_sections,axis = 0):
'''
Split an array into multiple sub-arrays.
'''
Parameters-------------
ary : ndarray---Array to be divided into sub-arrays.
indices_or_sections---int or 1-D array If `indices_or_sections` is an integer, N, the array will be divided into N equal arrays along `axis`. If such a split is not possible,
an error is raised.
If `indices_or_sections` is a 1-D array of sorted integers, the entries indicate where along `axis` the array is split. For example,``[2, 3]`` would, for ``axis=0``, result in
ary[:2]
ary[2:3]
ary[3:]
If an index exceeds the dimension of the array along `axis`,an empty sub-array is returned correspondingly.
axis:int,optional---The axis along which to split,default is 0.
0按列分割,1按行分割
Return:sub-array:list of ndarrays
A list of sub-arrays
example:
3、训练SVM
kernel='linear'时,为线性核,C越大分类效果越好,但有可能出现过拟合;
kernel='rbf'时,为高斯核,gamma越小,分类界面越连续;gamma越大,分类界面越分散,分类效果越好(训练集),但是有可能会过拟合。
decision_function_shape='ovr'时(one v rest),即一个类别与其他类别进行划分;
decision_function_shape='ovo'时(one v one),即将类别两两之间进行划分,用二分类的方法模拟多分类的结果。
*准确率计算方式
本文来自博客园,作者:爱吃帮帮糖,转载请注明原文链接:https://www.cnblogs.com/monkeyT/p/10471459.html