import numpy #从sklearn包自带的数据集中读出鸢尾花数据集data from sklearn.datasets import load_iris data = load_iris() #查看data类型,包含哪些数据 print('数据类型:',type(data)) print('数据内容:',data.keys()) #取出鸢尾花特征和鸢尾花类别数据,查看其形状及数据类型 iris_feature = data['feature_names'],data['data'] print('鸢尾花数据:',iris_feature) iris_target = data.target,data.target_names print('鸢尾花形状类别:',iris_target) #取出所有花的花萼长度(cm)的数据 sepal_length = numpy.array(list(len[0] for len in data['data'])) print('所有花萼长度:',sepal_length) #取出所有花的花瓣长度(cm)+花瓣宽度(cm)的数据 petal_length = numpy.array(list(len[2] for len in data['data'])) petal_length.resize(5,30) petal_width = numpy.array(list(len[3] for len in data['data'])) petal_width.resize(5,30) iris_lens = (petal_length,petal_width) print('所有花瓣长宽:',iris_lens) #取出某朵花的四个特征及其类别 print('特征:',data['data'][0]) print('类别:',data['target'][0]) #将所有花的特征和类别分成三组,每组50个 # 建立每种花的相应列表,存放数据 iris_setosa = [] iris_versicolor = [] iris_virginica = [] # 用for循环分类,根据观察可知当target为0时对应setosa类型,1为versicolor,2为virginica for i in range(0,150): if data['target'][i] == 0: # 类别为0的即为setosa,生成一条0为setosa类的鸢尾花花数据 data1 = data['data'][i].tolist() data1.append('setosa') iris_setosa.append(data1) elif data['target'][i] == 1: # 类别为1的即为versicolor,生成一条1为versicolor类的鸢尾花数据 data1 = data['data'][i].tolist() data1.append('versicolor') iris_versicolor.append(data1) else: #剩下类别为2的归为virginica data1 = data['data'][i].tolist() data1.append('virginica') iris_virginica.append(data1) #生成新的数组,每个元素包含四个特征+类别 datas = (iris_setosa,iris_versicolor,iris_virginica) print('新数组分类结果:',datas)
运行结果: