import numpy as np
import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm
from sklearn.model_selection import train_test_split def load_data_classfication():
'''
加载用于分类问题的数据集
'''
# 使用 scikit-learn 自带的 iris 数据集
iris=datasets.load_iris()
X_train=iris.data
y_train=iris.target
# 分层采样拆分成训练集和测试集,测试集大小为原始数据集大小的 1/4
return train_test_split(X_train, y_train,test_size=0.25,random_state=0,stratify=y_train) #支持向量机线性分类LinearSVC模型
def test_LinearSVC(*data):
X_train,X_test,y_train,y_test=data
cls=svm.LinearSVC()
cls.fit(X_train,y_train)
print('Coefficients:%s, intercept %s'%(cls.coef_,cls.intercept_))
print('Score: %.2f' % cls.score(X_test, y_test)) # 生成用于分类的数据集
X_train,X_test,y_train,y_test=load_data_classfication()
# 调用 test_LinearSVC
test_LinearSVC(X_train,X_test,y_train,y_test)

def test_LinearSVC_loss(*data):
'''
测试 LinearSVC 的预测性能随损失函数的影响
'''
X_train,X_test,y_train,y_test=data
losses=['hinge','squared_hinge']
for loss in losses:
cls=svm.LinearSVC(loss=loss)
cls.fit(X_train,y_train)
print("Loss:%s"%loss)
print('Coefficients:%s, intercept %s'%(cls.coef_,cls.intercept_))
print('Score: %.2f' % cls.score(X_test, y_test)) # 调用 test_LinearSVC_loss
test_LinearSVC_loss(X_train,X_test,y_train,y_test)

def test_LinearSVC_L12(*data):
'''
测试 LinearSVC 的预测性能随正则化形式的影响
'''
X_train,X_test,y_train,y_test=data
L12=['l1','l2']
for p in L12:
cls=svm.LinearSVC(penalty=p,dual=False)
cls.fit(X_train,y_train)
print("penalty:%s"%p)
print('Coefficients:%s, intercept %s'%(cls.coef_,cls.intercept_))
print('Score: %.2f' % cls.score(X_test, y_test)) # 调用 test_LinearSVC_L12
test_LinearSVC_L12(X_train,X_test,y_train,y_test)

def test_LinearSVC_C(*data):
'''
测试 LinearSVC 的预测性能随参数 C 的影响
'''
X_train,X_test,y_train,y_test=data
Cs=np.logspace(-2,1)
train_scores=[]
test_scores=[]
for C in Cs:
cls=svm.LinearSVC(C=C)
cls.fit(X_train,y_train)
train_scores.append(cls.score(X_train,y_train))
test_scores.append(cls.score(X_test,y_test)) ## 绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(Cs,train_scores,label="Traing score")
ax.plot(Cs,test_scores,label="Testing score")
ax.set_xlabel(r"C")
ax.set_ylabel(r"score")
ax.set_xscale('log')
ax.set_title("LinearSVC")
ax.legend(loc='best')
plt.show() # 调用 test_LinearSVC_C
test_LinearSVC_C(X_train,X_test,y_train,y_test)

吴裕雄 python 机器学习——支持向量机线性分类LinearSVC模型的更多相关文章

  1. 吴裕雄 python 机器学习——支持向量机SVM非线性分类SVC模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  2. 吴裕雄 python 机器学习——支持向量机非线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  3. 吴裕雄 python 机器学习——支持向量机线性回归SVR模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...

  4. 吴裕雄 python 机器学习——局部线性嵌入LLE降维模型

    # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...

  5. 吴裕雄 python 机器学习——数据预处理流水线Pipeline模型

    from sklearn.svm import LinearSVC from sklearn.pipeline import Pipeline from sklearn import neighbor ...

  6. 吴裕雄 python 机器学习——K均值聚类KMeans模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...

  7. 吴裕雄 python 机器学习——混合高斯聚类GMM模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import mixture from sklearn.metrics ...

  8. 吴裕雄 python 机器学习——超大规模数据集降维IncrementalPCA模型

    # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...

  9. 吴裕雄 python 机器学习——数据预处理正则化Normalizer模型

    from sklearn.preprocessing import Normalizer #数据预处理正则化Normalizer模型 def test_Normalizer(): X=[[1,2,3, ...

随机推荐

  1. layedit不可编辑,按钮不能使用

    $("#LAY_layedit_1").contents().find("body[contenteditable]").prop("contente ...

  2. Pytest学习9-常用插件

    pytest-django:为django应用程序编写测试. pytest-twisted:为twisted应用程序编写测试,启动反应堆并处理测试函数的延迟. pytest-cov:覆盖率报告,与分布 ...

  3. pytest学习8-运行上次执行失败的用例

    该插件提供了两个命令行选项,用于重新运行上次pytest调用的失败: --lf,--last-failed- 只重新运行上次失败的用例,如果没有失败则全部运行 --ff,--failed-first- ...

  4. 【C语言】猴子吃桃问题

    题目: 猴子第一天吃了若干个桃子,当即吃了一半,还不解馋,又多吃了一个: 第二天,吃剩下的桃子的一半,还不过瘾,又多吃了一个:以后每天都吃前一天剩下的一半多一个,到第10天想再吃时,只剩下一个桃子了. ...

  5. [USACO08JAN] 手机网络 - 树形dp

    经典问题系列 覆盖半径\(1\)的最小点覆盖集 \(f[i][0]\) 表示不在此处建信号塔,但\(i\)及其子树都有信号 \(f[i][1]\) 表示在此处建信号塔,但\(i\)及其子树都有信号 \ ...

  6. AntDesign(React)学习-13 Warning XX should not be prefixed with namespace XXX

    有篇UMI入门简易教程可以看看:https://www.yuque.com/umijs/umi/hello 程序在点击操作时报了一个Warning: [sagaEffects.put] User/up ...

  7. vue组件中的data为什么是函数?

    一.vue组件中的data为什么是函数 为了保证组件的独立性 和 可 复用性,data 是一个函数,组件实例化的时候这个函数将会被调用,返回一个对象,计算机会给这个对象分配一个内存地址,你实例化几次, ...

  8. Django | pycharm 提示 unresolved attribute referene 'objects' for class 'xxxx'

    objects高亮,提示信息为unresolved attribute referene 'objects' for class 'BookInfo' 当前情况是pycharm没有识别到objects ...

  9. NVMe over Fabrics 概况

    NVMe over Fabrics 技术特征 跨网络传输NVMe命令需要特殊的考虑,而不仅仅是针对本地存储内存.例如,为了远距离传输NVMe协议,理想的底层网络或fabric技术将具有以下特性: 可靠 ...

  10. 题解【AcWing275】[NOIP2008]传纸条

    题面 首先有一个比较明显的状态设计:设 \(dp_{x1,y1,x2,y2}\) 表示第一条路线走到 \((x1,y1)\) ,第二条路线走到 \((x2,y2)\) 的路径上的数的和的最大值. 这个 ...