python 数据科学
来源:互联网 发布:爱淘宝铅笔盒 编辑:程序博客网 时间:2024/05/19 12:39
from sklearn.datasets import load_irisfrom sklearn.svm import SVCfrom sklearn.linear_model import LogisticRegressioniris = load_iris()X = iris.data[0:100,[2,3]]Y = iris.target[0:100]'''支持向量机:分界线距离两个类别的边界最远'''clf1 = SVC(kernel="linear")clf1.fit(X, Y)clf2 = LogisticRegression()clf2.fit(X, Y)from itertools import productimport numpy as npimport matplotlib.pyplot as pltdef plot_estimator(estimator, X, Y): x0_min, x0_max = X[:, 0].min() - 1, X[:, 0].max() + 1 x1_min, x1_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x0_min, x0_max, 0.1), np.arange(x1_min, x1_max, 0.1)) Z = estimator.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.plot() plt.contourf(xx, yy, Z, alpha=0.4, cmap = plt.cm.RdYlBu) plt.scatter(X[:, 0], X[:, 1], c=Y, cmap = plt.cm.brg) plt.xlabel('Petal.Length') plt.ylabel('Petal.Width') plt.show()
plot_estimator(clf1, X, Y)plot_estimator(clf2, X, Y)
'''C, 对错误(边界的某些点)的容忍度,C值越大越不能容忍,宽度越小。C值越小间隔宽度越大。'''data = np.array([[-1,2,0],[-2,3,0],[-2,5,0],[-3,-4,0],[-0.1,2,0],[0.2,1,1],[0,1,1],[1,2,1], [1,1,1], [-0.4,0.5,1],[2,5,1]])X = data[:, :2] Y = data[:,2]# Large Marginclf = SVC(C=1.0, kernel='linear')clf.fit(X, Y)plot_estimator(clf,X,Y)# Narrow Marginclf = SVC(C=100000, kernel='linear')clf.fit(X, Y)plot_estimator(clf,X,Y)
SVM Kernels
from itertools import productimport numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets import load_irisfrom sklearn.svm import SVC'''rbf,不规则线poly,弧线linear,直线(非线性数据不好使用)'''iris = load_iris()X = iris.data[:,[2,3]]Y = iris.targetclf1 = SVC(kernel="rbf")clf1.fit(X, Y)clf2 = SVC(kernel="poly")clf2.fit(X, Y)clf3 = SVC(kernel="linear")clf3.fit(X, Y)def plot_estimator(estimator, X, Y, idx, title): x0_min, x0_max = X[:, 0].min() - 1, X[:, 0].max() + 1 x1_min, x1_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x0_min, x0_max, 0.1), np.arange(x1_min, x1_max, 0.1)) Z = estimator.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) axarr[idx].contourf(xx, yy, Z, alpha=0.4, cmap = plt.cm.RdYlBu) axarr[idx].scatter(X[:, 0], X[:, 1], c=Y, cmap = plt.cm.brg) axarr[idx].set_title(title) f, axarr = plt.subplots(1, 3, sharex='col', sharey='row', figsize=(20, 5))for idx, clf, title in zip([0,1,2],[clf1, clf2, clf3], ['rbf', 'poly', 'linear']): plot_estimator(clf, X, Y, idx, title)plt.show()
matplotlib.pyplot.subplots
matplotlib.pyplot
阅读全文
0 0
- Python 数据科学入门
- python数据科学手册
- python 数据科学
- python 数据科学
- python 数据科学
- python 数据科学
- python 数据科学
- python的数据科学资源
- Python数据科学学习进阶
- python用于数据科学教程
- python数据科学学习地址
- Python和数据科学的起步指南
- 数据科学的战争:R vs Python
- Python科学计算数据可视化模块-Matplotlib
- python科学计算数据可视化模块-seaborn
- Note:R+Python数据科学实战(1)
- Note:R+Python数据科学实战(2)
- Python数据科学方面的问题汇总
- ibatis insert 数组
- H5+JS 实现页面简单的气泡效果
- Android Selinux 实战
- Spring @Transactional 声明式事务管理 getCurrentSession
- JSP常用内置对象
- python 数据科学
- Python numpy函数:tile()
- 关于JRE和JDK的区别,终于知道他们的区别了
- 微信小程序如何检测接收iBeacon信号
- zuul微服务网关的常用示例
- SpringMVC的各种参数绑定方式
- Nginx https支持配置
- 提升效率的几个点
- php的强制转换