将Iris数据集的前五个观测值从数字映射到字符串

2024-09-27 00:16:27 发布

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

作为对机器学习的介绍,我必须从scikit学习库中找到与Iris数据集花朵对应的前五个名称

我不太确定如何处理这个问题,因为我在这个领域是全新的。有人告诉我可以做一些numpy索引来检索这些

我知道iris.target中的整数对应于0 = 'setosa'1 = 'versicolor'2 = 'virginica'

编辑: 为了澄清,我实际上想要实现的是将整数映射到iris.data中前5朵花的名称(将setosa、veriscolor或virginica分配给前5个观察值)


Tags: 数据numpy名称机器编辑iristarget整数
2条回答

是否要将数字转换为相应的类别?如果是,请尝试:

# Load first five flowers and store them in `y`
y = load_iris()['target'][:5]

# Declare dictionary to map each number to its corresponding text
dictionary = {0:'setosa',1:'versicolor',2:'virginica'}

# Translate each number to text using the dictionary
[dictionary[i] for i in y]

您可以对numpy.where执行相同的操作:

# Import numpy
import numpy as np

# Case-like structure
np.where(y == 0, 'setosa',
         np.where(y == 1, 'versicolor',
                  'virginica'))

如果可以的话,使用熊猫。很简单,

import pandas as pd
import numpy as np 

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
new_names = ['sepal_length','sepal_width','petal_length','petal_width','iris_class']

dataset = pd.read_csv(url, names=new_names, skiprows=0, delimiter=',') # load iris dataset from url

dataset.info() # gives details about your dataset

dataset.head() # this will give you first 5 entries in your dataset

# for more details
# check out this link
# https://medium.com/@yosik81/machine-learning-in-30-minutes-with-python-and-google-colab-6e6dfb77f5e1

相关问题 更多 >

    热门问题