pythonscikit-learniris-dataset

load_iris() got an unexpected keyword argument 'as_frame'


I tried to import the iris dataset to daraframe but it shows following error. I checked the scikit-learn documentation there is as_frame named parameter for load_iris().

My Code:

from sklearn.datasets import load_iris
df = load_iris(as_frame=True)

Error:

TypeError                                 Traceback (most recent call last)
<ipython-input-7-1f51689afac6> in <module>
      1 from sklearn.datasets import load_iris
----> 2 df = load_iris(as_frame=True)
      3 df

TypeError: load_iris() got an unexpected keyword argument 'as_frame'

Solution

  • This could be a good alternative:

    from sklearn.datasets import load_iris
    import pandas as pd
    
    data = load_iris()
    df = pd.DataFrame(data.data, columns=data.feature_names)
    df.head()