吴裕雄 python 机器学习——支持向量机线性分类LinearSVC模型
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模型的更多相关文章
- 吴裕雄 python 机器学习——支持向量机SVM非线性分类SVC模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...
- 吴裕雄 python 机器学习——支持向量机非线性回归SVR模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...
- 吴裕雄 python 机器学习——支持向量机线性回归SVR模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...
- 吴裕雄 python 机器学习——局部线性嵌入LLE降维模型
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...
- 吴裕雄 python 机器学习——数据预处理流水线Pipeline模型
from sklearn.svm import LinearSVC from sklearn.pipeline import Pipeline from sklearn import neighbor ...
- 吴裕雄 python 机器学习——K均值聚类KMeans模型
import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...
- 吴裕雄 python 机器学习——混合高斯聚类GMM模型
import numpy as np import matplotlib.pyplot as plt from sklearn import mixture from sklearn.metrics ...
- 吴裕雄 python 机器学习——超大规模数据集降维IncrementalPCA模型
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from sklearn import datas ...
- 吴裕雄 python 机器学习——数据预处理正则化Normalizer模型
from sklearn.preprocessing import Normalizer #数据预处理正则化Normalizer模型 def test_Normalizer(): X=[[1,2,3, ...
随机推荐
- 假期学习【十】首都之窗百姓信件JavaWweb+Echarts图表展示
今天主要对昨天爬取的数据进行处理,处理后用Echart图表展示, 效果如下:
- 链表问题----删除链表的中间节点和a/b处的节点
删除链表的中间节点和a/b处的节点 对于给定一个链表的头节点head,实现删除链表的中间节点的函数. 例如 不删除任何节点: 1->2,删除节点1 1->2->3,删除节点2 1-& ...
- 题解【洛谷P3406】海底高铁
题面 比较基础的前缀和+差分. 注意开\(\text{long long}\) 直接上代码吧. #include <bits/stdc++.h> #define itn int #defi ...
- nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比
转载自https://www.jianshu.com/p/7a8a7eb3707a 1.浏览器直接访问服务,获取到的 Host 包含浏览器请求的 IP 和端口 测试服务器,centos 7 sudo ...
- C# SDO_GEOMETRY
OracleParameter endGeometry = cmd.CreateParameter(); endGeometry.OracleDbType = OracleDbType.Object; ...
- 如何在macOS下安装geoserver
macOS 下的编译包 如果是使用安装文件,请查看官网文档,如果想要部署在已有的tomcat服务下,请查看网页压缩包章节. Web archive. An alternate way of insta ...
- Linux系统初学者的常见问题解决集结大全
http://www.embeddedlinux.org.cn/html/xinshourumen/200809/22-86.html 一. 如何建立多用户 提醒大家一句,别一直使用root用户,因为 ...
- 剖析Javascript中forEach()底层原理,如何重写forEach()
我们平时用的forEach()一般是这样用的 var myArr = [1,5,8] myArr.forEach((v,i)=>{ console.log(v,i) })//运行后是这样的1 0 ...
- [SUCTF 2019]EasySQL(堆叠注入配合sql_mode)
考点:1.堆叠注入 2.set sql_mode=PIPES_AS_CONCAT;将||视为字符串的连接操作符而非或运算符 意外:注入* 复现: 1;set sql_mode=PIPES_AS_CON ...
- 将用户名密码邮箱制成表格,以用户名为q结束
print("输入用户名.密码.邮箱长度不能超过20个") s="" while True: v = input("用户名:") if v= ...