1、np.ndenumerate、np.ndindex
import numpy as np
Z=np.arange(6).reshape(2,3)
Z
array([[0, 1, 2],
[3, 4, 5]])
for index,value in np.ndenumerate(Z):
print(index,value)
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
for index in np.ndindex(Z.shape):
print(index,Z[index])
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
for index in np.ndindex(2,2,3):
print(index)
(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 1, 0)
(0, 1, 1)
(0, 1, 2)
(1, 0, 0)
(1, 0, 1)
(1, 0, 2)
(1, 1, 0)
(1, 1, 1)
(1, 1, 2)
numpy.meshgrid()——生成网格点坐标矩阵
坐标矩阵——横坐标矩阵X XX中的每个元素,与纵坐标矩阵Y YY中对应位置元素,共同构成一个点的完整坐标
contour画等高线图
以z=x^2+y^2为例
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-10,10,100)
y=np.linspace(-10,10,100)
xx,yy=np.meshgrid(x,y)
Z=xx**2+yy**2
plt.contour(xx,yy,Z)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.linear_model import SGDClassifier
#生成一个50个样本点的数据集
X,y=make_blobs(n_samples=50,centers=2,random_state=0,cluster_std=0.6)
#训练模型
clf=SGDClassifier(loss='hinge',alpha=0.01,max_iter=200)
clf.fit(X,y)
print(clf.decision_function([[-1,-1]]))
#画决策超平面
xx=np.linspace(-1,5,10)
yy=np.linspace(-1,5,10)
X1,X2=np.meshgrid(xx,yy)
Z=np.zeros(X1.shape)
for (i,j),value in np.ndenumerate(X1):
x1=value
x2=X2[i,j]
p=clf.decision_function([[x1,x2]])
Z[i,j]=p[0]
levels=[-1.0,0,1.0]
plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.Paired,edgecolor='black')
linestyles=['dashed','solid','dashed']
plt.contour(X1,X2,Z,levels=levels,linestyles=linestyles,colors='k')
plt.axis('tight')
plt.show()
结果