作业12答案
代码如下
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs
center=[[1,1],[-1,-1],[1,-1]]
cluster_std=0.3
X,labels=make_blobs(n_samples=200,centers=center,n_features=2,
cluster\_std=cluster\_std,random\_state=0\)
print('X.shape',X.shape)
print("labels",set(labels))
unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))
for k,col in zip(unique_lables,colors):
x\_k=X\[labels==k\]
plt.plot\(x\_k\[:,0\],x\_k\[:,1\],'o',markerfacecolor=col,markeredgecolor="k",
markersize=14\)
plt.title('data by make_blob()')
plt.show()
#生成用于分类的数据集
from sklearn.datasets.samples_generator import make_classification
X,labels=make_classification(n_samples=200,n_features=2,n_redundant=0,n_informative=2,
random\_state=1,n\_clusters\_per\_class=2\)
rng=np.random.RandomState(2)
X+=2*rng.uniform(size=X.shape)
unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))
for k,col in zip(unique_lables,colors):
x\_k=X\[labels==k\]
plt.plot\(x\_k\[:,0\],x\_k\[:,1\],'o',markerfacecolor=col,markeredgecolor="k",
markersize=14\)
plt.title('data by make_classification()')
plt.show()
#生成球形判决界面的数据
from sklearn.datasets.samples_generator import make_circles
X,labels=make_circles(n_samples=200,noise=0.2,factor=0.2,random_state=1)
print("X.shape:",X.shape)
print("labels:",set(labels))
unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))
for k,col in zip(unique_lables,colors):
x\_k=X\[labels==k\]
plt.plot\(x\_k\[:,0\],x\_k\[:,1\],'o',markerfacecolor=col,markeredgecolor="k",
markersize=14\)
plt.title('data by make_moons()')
plt.show()