gpt4 book ai didi

python - 如何将 Scikit-learn 数据集转换为 Pandas 数据集

转载 作者:行者123 更新时间:2023-12-03 05:11:47 26 4
gpt4 key购买 nike

如何将数据从 Scikit-learn Bunch 对象转换为 Pandas DataFrame?

from sklearn.datasets import load_iris
import pandas as pd
data = load_iris()
print(type(data))
data1 = pd. # Is there a Pandas method to accomplish this?

最佳答案

手动,您可以使用 pd.DataFrame构造函数,给出一个 numpy 数组 ( data ) 和列名称列表 ( columns )。要将所有内容包含在一个 DataFrame 中,您可以使用 np.c_[...] 将特征和目标连接到一个 numpy 数组中。 (注意 [] ):

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris

# save load_iris() sklearn dataset to iris
# if you'd like to check dataset type use: type(load_iris())
# if you'd like to view list of attributes use: dir(load_iris())
iris = load_iris()

# np.c_ is the numpy concatenate function
# which is used to concat iris['data'] and iris['target'] arrays
# for pandas column argument: concat iris['feature_names'] list
# and string list (in this case one string); you can make this anything you'd like..
# the original dataset would probably call this ['Species']
data1 = pd.DataFrame(data= np.c_[iris['data'], iris['target']],
columns= iris['feature_names'] + ['target'])

关于python - 如何将 Scikit-learn 数据集转换为 Pandas 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38105539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com