scikit-learn:matplotlib.pyplot常用画图功能总结(1)

参考:http://matplotlib.org/api/pyplot_api.html

画图功能总结(2):http://blog.csdn.net/mmc2015/article/details/48222611


1、matplotlib.pyplot.plot(*args**kwargs),最简单的沿坐标轴划线函数

下面四种格式都合法:

plot(x, y)        # plot x and y using default line style and color
plot(x, y, 'bo')  # plot x and y using blue circle markers
plot(y)           # plot y using x as index array 0..N-1
plot(y, 'r+')     # ditto, but with red plusses

<pre name="code" class="python">import numpy as np
import matplotlib.pyplot as plt

x1=np.arange(0,5,0.1)
y1=np.sin(x1)
x2=np.linspace(1,10,20,True)
y2=np.cos(x2)

plt.plot(x1,y1,'b^')

 
 

也可以同时画一组图:

plt.plot(x1, y1, 'go', x2, y2, 'r-')

如果颜色不显示指出,则默认循环使用不同的颜色,支持的颜色有:

character color
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan
‘m’ magenta
‘y’ yellow
‘k’ black
‘w’ white
支持的line style有:

character description
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle_down marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3'
  • 11
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
)? 好的,下面是支持向量机分类鸢尾花数据集代码(不用Scikit-Learn库): ```python import numpy as np import matplotlib.pyplot as plt # 加载鸢尾花数据集 f = open('iris.data', 'r') data = f.readlines() f.close() X = [] Y = [] for i in range(len(data)): line = data[i].split(',') x = [] for j in range(len(line)-1): x.append(float(line[j])) X.append(x) if line[-1].strip() == 'Iris-setosa': Y.append(0) else: Y.append(1) X = np.array(X) Y = np.array(Y) # 定义SVM类 class SVM: def __init__(self, C=1.0): self.C = C # 计算核函数 def kernel(self, x1, x2): return np.dot(x1, x2) # 计算alpha def train(self, X, Y): n_samples, n_features = X.shape # 初始化alpha和b alpha = np.zeros(n_samples) b = 0 # 计算Gram矩阵和二次规划问题的参数 K = np.zeros((n_samples, n_samples)) for i in range(n_samples): for j in range(n_samples): K[i,j] = self.kernel(X[i], X[j]) P = np.outer(Y, Y) * K q = -np.ones(n_samples) G = np.vstack((-np.eye(n_samples), np.eye(n_samples))) h = np.hstack((np.zeros(n_samples), np.ones(n_samples) * self.C)) # 求解二次规划问题 from cvxopt import matrix, solvers P = matrix(P) q = matrix(q) G = matrix(G) h = matrix(h) solvers.options['show_progress'] = False sol = solvers.qp(P, q, G, h) alpha = np.array(sol['x']).flatten() # 计算b for i in range(n_samples): if alpha[i] > 1e-7: b = Y[i] - np.sum(alpha * Y * K[i,:]) break self.alpha = alpha self.b = b # 预测 def predict(self, x): res = 0 for i in range(len(self.alpha)): res += self.alpha[i] * self.kernel(x, X[i]) res += self.b return np.sign(res) # 训练模型 clf = SVM() clf.train(X, Y) # 画图 colors = ['red', 'blue'] for i in range(len(Y)): plt.scatter(X[i][0], X[i][1], c=colors[Y[i]]) xlim = plt.xlim() ylim = plt.ylim() xx, yy = np.meshgrid(np.linspace(xlim[0], xlim[1], 50),\ np.linspace(ylim[0], ylim[1], 50)) Z = np.zeros(xx.shape) for i in range(xx.shape[0]): for j in range(xx.shape[1]): Z[i,j] = clf.predict([xx[i,j], yy[i,j]]) plt.contour(xx, yy, Z, colors='black', levels=[-1, 0, 1]) plt.show() ``` 我已经完成了你的第一个问题,接下来还有其它问题吗?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值