sklearn.datasets.fetch_20newsgroups(*, data_home=None, subset='train', categories=None, shuffle=True, random_state=42, remove=(), download_if_missing=True, return_X_y=False)[source]

Load the filenames and data from the 20 newsgroups dataset (classification).

Download it if necessary.



Samples total






Read more in the User Guide.

data_homeoptional, default: None

Specify a download and cache folder for the datasets. If None, all scikit-learn data is stored in ‘~/scikit_learn_data’ subfolders.

subset‘train’ or ‘test’, ‘all’, optional

Select the dataset to load: ‘train’ for the training set, ‘test’ for the test set, ‘all’ for both, with shuffled ordering.

categoriesNone or collection of string or unicode

If None (default), load all the categories. If not None, list of category names to load (other categories ignored).

shufflebool, optional

Whether or not to shuffle the data: might be important for models that make the assumption that the samples are independent and identically distributed (i.i.d.), such as stochastic gradient descent.

random_stateint, RandomState instance, default=None

Determines random number generation for dataset shuffling. Pass an int for reproducible output across multiple function calls. See Glossary.


May contain any subset of (‘headers’, ‘footers’, ‘quotes’). Each of these are kinds of text that will be detected and removed from the newsgroup posts, preventing classifiers from overfitting on metadata.

‘headers’ removes newsgroup headers, ‘footers’ removes blocks at the ends of posts that look like signatures, and ‘quotes’ removes lines that appear to be quoting another post.

‘headers’ follows an exact standard; the other filters are not always correct.

download_if_missingoptional, True by default

If False, raise an IOError if the data is not locally available instead of trying to download the data from the source site.

return_X_ybool, default=False.

If True, returns (, instead of a Bunch object.

New in version 0.22.


Dictionary-like object, with the following attributes.

datalist, length [n_samples]

The data list to learn.

target: array, shape [n_samples]

The target labels.

filenames: list, length [n_samples]

The path to the location of the data.

DESCR: str

The full description of the dataset.

target_names: list, length [n_classes]

The names of target classes.

(data, target)tuple if return_X_y=True

New in version 0.22.