3

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'
Harsh Kothari
  • 55
  • 1
  • 9
  • 6
    It requires scikit learn version 0.23 or up, so you need to update your scikit learn. See [here](https://stackoverflow.com/questions/33679938/how-to-upgrade-scikit-learn-package-in-anaconda) if you are using anaconda. – Mustafa Aydın Jun 11 '20 at 11:06

3 Answers3

7

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()
3

You are using an older version of sklearn, that is why you are getting an error. To fix this, just install a version of sklearn >= 0.23

For Example:

pip install scikit-learn==0.23

Sklearn docs:

as_framebool, default=False If True, the data is a pandas DataFrame including columns with appropriate dtypes (numeric). The target is a pandas DataFrame or Series depending on the number of target columns. If return_X_y is True, then (data, target) will be pandas DataFrames or Series as described below.

New in version 0.23.

Valdas
  • 198
  • 1
  • 7
1

or if you want to showing target too:

from sklearn.datasets import load_iris
import pandas as pd

pd.set_option("max_columns", None)
pd.set_option("max_rows", None)

data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df.insert(4, "target", data.target, allow_duplicates=False)
df

pd.set_option() is used to display columns and rows as a whole

Nasir Usman
  • 61
  • 1
  • 4