How to convert a Scikit-learn dataset to a Pandas dataset?

后端 未结 22 2182
清酒与你
清酒与你 2020-11-28 19:10

How do I convert data from a Scikit-learn Bunch object to a Pandas DataFrame?

from sklearn.datasets import load_iris
import pandas as pd
data = load_iris()
p         


        
22条回答
  •  误落风尘
    2020-11-28 19:43

    Working off the best answer and addressing my comment, here is a function for the conversion

    def bunch_to_dataframe(bunch):
      fnames = bunch.feature_names
      features = fnames.tolist() if isinstance(fnames, np.ndarray) else fnames
      features += ['target']
      return pd.DataFrame(data= np.c_[bunch['data'], bunch['target']],
                     columns=features)
    

提交回复
热议问题