Iris数据集以鸢尾花的特征作为数据来源,数据集包含150个数据集,有4维,分为3 类(setosa、versicolour、virginica),每类50个数据,每个数据包含4个属性,花萼长度、宽度和花瓣长度、宽度。
from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np
import math
#prepare the data
iris = datasets.load_iris()
X = iris.data
y = iris.target
names = iris.feature_names
labels = iris.target_names
y_c = np.unique(y)
"""visualize the distributions of the four different features in 1-dimensional histograms"""
fig, axes = plt.subplots(2, 2, figsize=(12, 6))
for ax, column in zip(axes.ravel(), range(X.shape[1])):
# set bin sizes
min_b = math.floor(np.min(X[:, column]))
max_b = math.ceil(np.max(X[:, column]))
bins = np.linspace(min_b, max_b, 25)
# plotting the histograms
for i, color in zip(y_c, ('blue', 'red', 'green')):
ax.hist(X[y == i, column], color=color, label='class %s' % labels[i],
bins=bins, alpha=0.5, )
ylims = ax.get_ylim()
# plot annotation
l = ax.legend(loc='upper right', fancybox=True, fontsize=8)
l.get_frame().set_alpha(0.5)
ax.set_ylim([0, max(ylims) + 2])
ax.set_xlabel(names[column])
ax.set_title('Iris histogram feature %s' % str(column + 1))
# hide axis ticks
ax.tick_params(axis='both', which='both', bottom=False, top=False, left=False, right=False,
labelbottom=True, labelleft=True)
# remove axis spines
ax.spines['top'].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
axes[0][0].set_ylabel('count')
axes[1][0].set_ylabel('count')
fig.tight_layout()
plt.show()
得到的结果为
原创文章 19获赞 35访问量 1818
关注
私信
展开阅读全文
作者:混混度日的咸鱼