import numpy from sklearn.datasets import load_iris #从sklearn包自带的数据集中读出鸢尾花数据集data
iris_data = load_iris() # 查看data类型,包含哪些数据 print("数据类型: ", type(iris_data)) print("包含数据: ", iris_data.keys()) # 看包含哪些数据
iris_feature = data.feature_names,data.data #鸢尾花特征: print(iris_feature) #iris_feature数据类型 print(type(iris_feature)) iris_target = data.target #鸢尾花数据类别: print(iris_target) #iris_target数据类型: print(type(iris_target))
sepal_len = np.array(list(len[0] for len in data.data)) #取出所有花的花萼长度(cm)的数据
print(sepal_len)
# 6.取出所有花的花瓣长度(cm)+花瓣宽度(cm)的数据 petal_len = numpy.array(list(len[2] for len in iris_data['data'])) # 取花瓣长 petal_len.resize(5, 30) petal_wid = numpy.array(list(wid[3] for wid in iris_data['data'])) # 取花瓣宽 petal_wid.resize((5, 30)) petal_len_wid = numpy.array(dict(length=petal_len, width=petal_wid)) # 形成新数组 print("花瓣长宽: ", petal_len_wid)
# 取出某朵花的四个特征及其类别 print("某朵花数据: ", iris_data['data'][0], iris_data['target'][0])
iris_one = [] iris_two = [] iris_three = [] for i in range(0,150): if data.target[i] == 0: Data = data.data[i].tolist() Data.append('setose') iris_one.append(Data) elif data.target[i] ==1: Data = data.data[i].tolist() Data.append('color') iris_two.append(Data) else: Data = data.data[i].tolist() Data.append('flower') iris_three.append(Data)
# 生成新的数组,每个元素包含四个特征+类别 iris_result = numpy.array([iris_setosa, iris_versicolor, iris_virginica]) print("分类结果", iris_result)