基于numy操作的虹膜分类ipython

2024-10-17 06:16:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我在看这本书Building Machine Learning Systems with Python。从scipy加载数据集后,我需要提取属于setosa的所有特性的索引。但我无法提取。可能是因为我没有使用numpy数组。有人能帮我提取索引号吗?以下代码

from matplotlib import pyplot as plt
from sklearn.datasets import load_iris

import numpy as np
# We load the data with load_iris from sklearn

data = load_iris()

features = data['data']

feature_names = data['feature_names']

target = data['target']
for t,marker,c in zip(xrange(3),">ox","rgb"):

# We plot each class on its own to get different colored markers
plt.scatter(features[target == t,0], features[target == t,1],
            marker=marker, c=c)

plength = features[:, 2]

# use numpy operations to get setosa features

is_setosa = (labels == 'setosa')

# This is the important step:

max_setosa = plength[is_setosa].max()

min_non_setosa = plength[~is_setosa].min()

print('Maximum of setosa: {0}.'.format(max_setosa))

print('Minimum of others: {0}.'.format(min_non_setosa))

Tags: fromimportnumpyiristargetdataiswith
1条回答
网友
1楼 · 发布于 2024-10-17 06:16:05

在问题行之前定义标签。在

target_names = data['target_names']
labels = target_names[target]

现在这些线路将正常工作:

^{pr2}$

额外的。 sklearn的数据束(Data=load_chiris())由0-2个数字的目标数组组成,这些数组和花的特征和意义有关。使用它可以提取属于setosa(其中target等于0)的所有特征,例如:

petal_length = features[:, 2]
setosa_petal_length = petal_length[target == 0]

用数据['target_names']来面对这个问题,你会在顶部得到两行,这是你问题的答案。顺便说一下,数据中的所有数组都是NumPy的ndarray。在

相关问题 更多 >