1 花瓣长度与宽度

import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt  
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target
plt.scatter(X[:, 2], X[:, -1], c = "red", marker='o')  
plt.xlabel('petal length')  
plt.ylabel('petal width')   
plt.show()  

87379774910835c5bd53bd09225c1d73.png
import pandas as pd
Iris=pd.DataFrame(X,columns=iris.feature_names)
Iris['species'] = y
import seaborn as sns
g = sns.PairGrid(Iris)   #  成对画散点图
g.map(plt.scatter)

6ebb927d8a00875a3918fc6b1deb2db8.png
g = sns.PairGrid(Iris, hue="species")
g.map_diag(plt.hist)   #   对角线
g.map_offdiag(plt.scatter)  #   非对角线
g.add_legend()

b26c90aaa880b174461321078042b600.png
#聚类后结果
from sklearn.cluster import KMeans
estimator = KMeans(n_clusters=3)
estimator.fit(X)
label_pred = estimator.labels_ 
x0 = X[label_pred == 0]
x1 = X[label_pred == 1]
x2 = X[label_pred == 2]
plt.scatter(x0[:, 2], x0[:, -1], c = "red", marker='o', label='label0')  
plt.scatter(x1[:, 2], x1[:, -1], c = "green", marker='*', label='label1')  
plt.scatter(x2[:, 2], x2[:, -1], c = "blue", marker='+', label='label2')  
plt.xlabel('petal length')  
plt.ylabel('petal width')  
plt.legend(loc=2)  
plt.show()  

6d11c4e78e9a3cdc42c377c3a5d00b8b.png
#原来数据的类别
x0 = X[y == 0]
x1 = X[y == 1]
x2 = X[y == 2]
plt.scatter(x0[:, 2], x0[:, -1], c = "red", marker='o', label='label0')  
plt.scatter(x1[:, 2], x1[:, -1], c = "green", marker='*', label='label1')  
plt.scatter(x2[:, 2], x2[:, -1], c = "blue", marker='+', label='label2')  
plt.xlabel('petal length')  
plt.ylabel('petal width')  
plt.legend(loc=2)  
plt.show() 

0a9a83087c7cedc5b4dc54fcf795135c.png

2 花萼长度与宽度

plt.scatter(X[:, 0], X[:, 1], c = "red", marker='o')  
plt.xlabel('sepal length')  
plt.ylabel('sepal width')   
plt.show() 

2d7cbeeb4c15a08ca7e4e9281c2f54db.png
# 聚类后结果
from sklearn.cluster import KMeans
estimator = KMeans(n_clusters=3)
estimator.fit(X)
label_pred = estimator.labels_ 
x0 = X[label_pred == 0]
x1 = X[label_pred == 1]
x2 = X[label_pred == 2]
plt.scatter(x0[:, 0], x0[:, 1], c = "red", marker='o', label='label0')  
plt.scatter(x1[:, 0], x1[:, 1], c = "green", marker='*', label='label1')  
plt.scatter(x2[:, 0], x2[:, 1], c = "blue", marker='+', label='label2')  
plt.xlabel('petal length')  
plt.ylabel('petal width')  
plt.legend(loc=2)  
plt.show()

1c0aac690e4c07f35ecb360cdf968afe.png
# 原来数据的类别
x0 = X[label_pred == 0]
x1 = X[label_pred == 1]
x2 = X[label_pred == 2]
plt.scatter(x0[:, 1], x0[:, 2], c = "red", marker='o', label='label0')  
plt.scatter(x1[:, 1], x1[:, 2], c = "green", marker='*', label='label1')  
plt.scatter(x2[:, 1], x2[:, 2], c = "blue", marker='+', label='label2')  
plt.xlabel('petal length')  
plt.ylabel('petal width')  
plt.legend(loc=2)  
plt.show()  

4a9a9085ed7f15212ebe20d0967f9c87.png
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐