# 3维 import numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets.samples_generator import make_classificationfrom mpl_toolkits.mplot3d import Axes3Dfig = plt.figure() ax = Axes3D(fig) data,labels=make_classification(n_samples=1000,n_features=3,n_redundant=0,n_informative=2, random_state=1,n_clusters_per_class=2)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=data[labels==k] ax.scatter3D(x_k[:,0],x_k[:,1],x_k[:, 2], c=col) # 开始绘制,x_k[:,0] 表示取第一维plt.title('data by make_classification()')plt.show()
# 2维
import numpy as npimport matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_classification
data,labels=make_classification(n_samples=100,n_features=2,n_redundant=0,n_informative=2, random_state=5,n_clusters_per_class=2)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=data[labels==k] plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor="k", markersize=10)plt.title('data by make_classification()')plt.show()