How to import covtype dataset in sklearn?

In scikit-learn (sklearn), the fetch_covtype() method is used to load & retrieve the Covtype dataset. The Covtype dataset is a commonly used benchmark dataset in machine learning and is often used for classification tasks.

The Covtype dataset contains information about forest cover types in the Roosevelt National Forest in Colorado, USA. It includes cartographic variables such as elevation, slope, and aspect, as well as information about soil type, wilderness area, and other features.

From sklearn datasets module, fetch_covtype method is used to download and then load the Covtype dataset into Python environment. It helps to easily access the dataset for analysis and ML model training. In addition, this dataset is usually used to train machine learning models for classification tasks.


The fetch_covtype() method in scikit-learn’s datasets module takes several parameters to customize its behavior. Here are some useful parameters:

  • data_home: (optional) Shows the directory where the dataset should be stored. If not provided, the dataset will be stored in a default location.
  • download_if_missing: (optional) If dataset does not exist in the data_home directory, it will be download. The default value is True, that means the dataset will be automatically downloaded when needed.
  • random_state: (optional) Sets the random seed for shuffling the dataset. It can be an integer or an instance of the RandomState class.
  • return_X_y: (optional) Specifies whether to return the features and target variables separately (True) or as a single object (False). Default= False, shows the function returns a single object containing both features and target.
  • as_frame: (optional) Specifies whether to return a pandas DataFrame (True) or numpy arrays (False) for the features and target variables. The default value is False.

Return Value

By default, when return_X_y is set to False (which is the default value), the fetch_covtype method returns a single object that contains both the features and target variables. This object typically has attributes such as data and target that is accessed to retrieve the features and target data.

Alternatively, when return_X_y is set to True, the method returns the features and target variables separately as two distinct outputs. In this case, the features are returned as the first output (usually denoted as X), and the target variable is returned as the second output (usually denoted as y).


Here we have a code explanation.

from sklearn.datasets import fetch_covtype

# Load the Covtype dataset
dataset = fetch_covtype(data_home='/path/to/dataset', download_if_missing=True, random_state=42, return_X_y=True, as_frame=False)

# Access the features and target variables
X, y =,

In this example, the Covtype dataset will be downloaded (if necessary) and stored in the /path/to/dataset directory. The random seed is set to 42, and the features and target variables are returned as separate numpy arrays (X and y, respectively).

Here’s we learn more articles:

  1. California housing price dataset in sklearn
  2. fetch_20newsgroups method in sklearn

Stay in the Loop

Get the weekly email from Algoideas that makes reading the AI/ML stuff instructive. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...