figure1.py

pythonimport numpy as np
from sklearn.datasets import load_iris
from matplotlib import pyplot as plt

data = load_iris()
features = data['data']
feature_names = data['feature_names']
target = data['target']

pairs = [(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)] #指的是四个参数的摆列组合
for i,(p0,p1) in enumerate(pairs):
    plt.subplot(2,3,i+1) #画一个大图包含若干个小图,第一个参数代表行数,第二个参数代表列数,带三个参数代表每个图的id
    for t,marker,c in zip(range(3),">ox","rgb"): #zip用于创造元组,然后给t,marker,c遍历
         plt.scatter(features[target == t,p0], features[target == t,p1], marker=marker, c=c) #marker指的是形状,c指的是颜色 选取参数和花的样子进行绘图
    plt.xlabel(feature_names[p0]) #x轴标签
    plt.ylabel(feature_names[p1])
    plt.xticks([]) #x轴的长度范围
    plt.yticks([])
plt.savefig('../1400_02_01.png') #存储图像

figure2.py

pythonCOLOUR_FIGURE = False

from matplotlib import pyplot as plt
from sklearn.datasets import load_iris
data = load_iris()
features = data['data']
feature_names = data['feature_names']
species = data['target_names'][data['target']]

setosa = (species == 'setosa') #返回的是一坨true和false
features = features[~setosa]
species = species[~setosa]
virginica = species == 'virginica'

t = 1.75 #之前选择出来的阀值用于区分
p0,p1 = 3,2 #四个参数中的3和2这两个参数

if COLOUR_FIGURE:
    area1c = (1.,.8,.8)
    area2c = (.8,.8,1.)
else:
    area1c = (1.,1,1)
    area2c = (.7,.7,.7)

x0,x1 =[features[:,p0].min()*.9,features[:,p0].max()*1.1] 
#乘以0.9 和 1.1 的目的在于让坐标轴比实际的数据大一点
y0,y1 =[features[:,p1].min()*.9,features[:,p1].max()*1.1]

plt.fill_between([t,x1],[y0,y0],[y1,y1],color=area2c)
plt.fill_between([x0,t],[y0,y0],[y1,y1],color=area1c)
#fill_between的后面两个坐标参数y1和y2不太好理解,大概指的是填充的y1是下界,y2是上界,这里是一条直线来表示的其它的图片可以是sin和cos之类的玩意
plt.plot([t,t],[y0,y1],'k--',lw=2)
plt.plot([t-.1,t-.1],[y0,y1],'k:',lw=2)
#这两行代码的作用是画两条分界线用的
plt.scatter(features[virginica,p0], features[virginica,p1], c='b', marker='o')
plt.scatter(features[~virginica,p0], features[~virginica,p1], c='r', marker='x')
plt.ylim(y0,y1)
plt.xlim(x0,x1)
plt.xlabel(feature_names[p0])
plt.ylabel(feature_names[p1])
plt.savefig('../1400_02_02.png')

未完待续


MrZONT
741 声望92 粉丝