赞
踩
目录
基于朴素贝叶斯的文本分类实现参见我的博客:
NLP学习计划(四):朴素贝叶斯原理及文本分类的sklearn实现:https://blog.csdn.net/weixin_42483560/article/details/89296776
直接贴上自己的SVM(参考书籍为西瓜书及其他文档)学习笔记:


scikit-learn中SVM的算法库分为分类的三个类:SVC, NuSVC,和Linear。和回归类的三个类:SVR, NuSVR,和LinearSVR 。相关的函数类都包裹在sklearn.svm模块之中。
SVC和 NuSVC差不多,区别仅仅在于对损失的度量方式不同,而LinearSVC从名字就可以看出,他是线性分类,也就是不支持各种低维到高维的核函数,仅仅支持线性核函数,对线性不可分的数据不能使用。
对于SVR, NuSVR,和LinearSVR 3个回归的类,三者特别同上。
如果有经验知道数据是线性可以拟合的,那么使用LinearSVC去分类 或者LinearSVR去回归,它们不需要我们去慢慢的调参去选择各种核函数以及对应参数, 速度也快。如果我们对数据分布没有什么经验,一般使用SVC去分类或者SVR去回归,这就需要我们选择核函数以及对核函数调参了。
而对于NuSVC和NuSVR,如果我们对训练集训练的错误率或者说支持向量的百分比有要求的时候,可以选择NuSVC分类 和 NuSVR 。它们有一个参数来控制这个百分比。
在scikit-learn中内置的核函数一共有4种:
1.线性核函数(Linear Kernel)表达式为:K(x,z)=x∙zK(x,z)=x∙z,就是普通的内积。
2.多项式核函数(Polynomial Kernel)是线性不可分SVM常用的核函数之一,表达式为:K(x,z)=(γx∙z+r)dK(x,z)=(γx∙z+r)d ,其中,γ,r,dγ,r,d都需要自己调参定义,比较麻烦。
3.高斯核函数(Gaussian Kernel),在SVM中也称为径向基核函数(Radial Basis Function,RBF),它是sklearn库和libsvm库默认的核函数。表达式为:K(x,z)=exp(−γ||x−z||2)K(x,z)=exp(−γ||x−z||2), 其中,γ大于0,需要自己调参定义。般情况下,对非线性数据使用默认的高斯核函数会有比较好的效果,
4.Sigmoid核函数(Sigmoid Kernel)也是线性不可分SVM常用的核函数之一,表达式为:K(x,z)=tanh(γx∙z+r)K(x,z)=tanh(γx∙z+r), 其中,γ,rγ,r都需要自己调参定义。
参考链接:https://www.cnblogs.com/pinard/p/6117515.html
- import pandas as pd
- from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
- from sklearn.model_selection import train_test_split
- from sklearn.svm import SVC
- from sklearn.metrics import classification_report
- from sklearn.model_selection import *
-
- # 导入数据
- train_data= pd.read_csv('F:\PY-Learning\CNEWS\cnews\cnews.train.txt', names=['title', 'content'], sep='\t', engine='python', encoding='UTF-8') # (50000, 2)
- test_data = pd.read_csv('F:\PY-Learning\CNEWS\cnews\cnews.test.txt', names=['title', 'content'], sep='\t',engine='python',encoding='UTF-8') # (10000, 2)
- val_data = pd.read_csv('F:\PY-Learning\CNEWS\cnews\cnews.val.txt', names=['title', 'content'], sep='\t',engine='python',encoding='UTF-8') # (5000, 2)
-
- x_train = train_data['content']
- x_test = test_data['content']
- x_val = val_data['content']
-
- y_train = train_data['title']
- y_test = test_data['title']
- y_val = val_data['title']
- # print(y_val)
- ###################################################
- #############处理样本#################################
-
- ## 默认不去停用词的向量化
- count_vec = CountVectorizer()
- x_count_train = count_vec.fit_transform(x_train )
- x_count_test = count_vec.transform(x_test )
-
- ## 去除停用词
- count_stop_vec = CountVectorizer(analyzer='word', stop_words='english')
- x_count_stop_train = count_stop_vec.fit_transform(x_train)
- x_count_stop_test = count_stop_vec.transform(x_test)
-
- ## 模型训练
- mnb_count = SVC()
- mnb_count.fit(x_count_train, y_train)
- mnb_count_y_predict = mnb_count.predict(x_count_test)
- mnb_count.score(x_count_test, y_test)
-
- ## TF−IDF处理后在训练
- ## 默认配置不去除停用词
- tfid_vec = TfidfVectorizer()
- x_tfid_train = tfid_vec.fit_transform(x_train)
- x_tfid_test = tfid_vec.transform(x_test)
-
- ## 模型训练
- mnb_tfid = SVC()
- mnb_tfid.fit(x_tfid_train, y_train)
- mnb_tfid_y_predict = mnb_tfid.predict(x_tfid_test)
- mnb_tfid.score(x_tfid_test, y_test)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。