Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

I have used the following code to convert the sk learn breast cancer data set to data frame : I am not getting the output ? I am very new in python and not able to figure out what is wrong.

def answer_one(): 

    import numpy as np
    import pandas as pd
    from sklearn.datasets import load_breast_cancer 
    cancer = load_breast_cancer()     
    data = numpy.c_[cancer.data, cancer.target]
    columns = numpy.append(cancer.feature_names, ["target"])
    return pandas.DataFrame(data, columns=columns)

answer_one()
like image 660
solly bennet Avatar asked Feb 13 '18 14:02

solly bennet


1 Answers

The following code works

def answer_one(): 
    import numpy as np
    import pandas as pd
    from sklearn.datasets import load_breast_cancer 
    cancer = load_breast_cancer()     
    data = np.c_[cancer.data, cancer.target]
    columns = np.append(cancer.feature_names, ["target"])
    return pd.DataFrame(data, columns=columns)

answer_one()

The reason why your code doesn't work before was you try to call numpy and pandas package again after defining it as np and pd respectively.

However, i suggest that the package loading and redefinition is done at the beginning of the script, outside a function definition.

like image 154
Michael L. Tanny Avatar answered Sep 23 '22 19:09

Michael L. Tanny