当前位置: 首页>>技术问答>>正文


如何将Sklearn数据集转换为Pandas数据集?

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

from sklearn.datasets import load_iris
import pandas as pd
data = load_iris()
print(type(data))    #输出:<class 'sklearn.utils.Bunch'>
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'])

 

第二种思路

对于scikit-learn中的所有数据集,上文”最佳思路”的解决方案不够通用。例如,它不适用于波士顿住房数据集。我提出了另一种更通用的解决方案。也无需使用numpy。

from sklearn import datasets
import pandas as pd

boston_data = datasets.load_boston()
df_boston = pd.DataFrame(boston_data.data,columns=boston_data.feature_names)
df_boston['target'] = pd.Series(boston_data.target)
df_boston.head()

作为通用函数:

def sklearn_to_df(sklearn_dataset):
    df = pd.DataFrame(sklearn_dataset.data, columns=sklearn_dataset.feature_names)
    df['target'] = pd.Series(sklearn_dataset.target)
    return df

df_boston = sklearn_to_df(datasets.load_boston())

sklearn iris 数据集


参考资料

 

本文由《纯净天空》出品。文章地址: https://vimsky.com/article/4362.html,未经允许,请勿转载。