【问题标题】:Bunch object not callable - scikit-learn rcv1 datasetBunch 对象不可调用 - scikit-learn rcv1 数据集
【发布时间】:2019-08-14 16:41:00
【问题描述】:

我想拆分 RCV1 内置数据集的训练集和测试集并应用 k-means 算法,但是在尝试拆分数据时,会显示一个错误,说 bundle object not callable

from sklearn.datasets import fetch_rcv1
rcv1 = fetch_rcv1()

x_train = rcv1(subset='train')

【问题讨论】:

    标签: scikit-learn dataset train-test-split


    【解决方案1】:

    确实不是;也不是数据框 - 请参阅docsDESCR 属性中包含一些额外信息:

    from sklearn.datasets import fetch_rcv1
    rcv1 = fetch_rcv1()
    
    print(rcv1.DESCR)
    

    结果:

    .. _rcv1_dataset:
    
    RCV1 dataset
    ------------
    
    Reuters Corpus Volume I (RCV1) is an archive of over 800,000 manually 
    categorized newswire stories made available by Reuters, Ltd. for research 
    purposes. The dataset is extensively described in [1]_.
    
    **Data Set Characteristics:**
    
        ==============     =====================
        Classes                              103
        Samples total                     804414
        Dimensionality                     47236
        Features           real, between 0 and 1
        ==============     =====================
    
    :func:`sklearn.datasets.fetch_rcv1` will load the following 
    version: RCV1-v2, vectors, full sets, topics multilabels::
    
        >>> from sklearn.datasets import fetch_rcv1
        >>> rcv1 = fetch_rcv1()
    
    It returns a dictionary-like object, with the following attributes:
    
    ``data``:
    The feature matrix is a scipy CSR sparse matrix, with 804414 samples and
    47236 features. Non-zero values contains cosine-normalized, log TF-IDF vectors.
    A nearly chronological split is proposed in [1]_: The first 23149 samples are
    the training set. The last 781265 samples are the testing set. This follows 
    the official LYRL2004 chronological split. The array has 0.16% of non zero 
    values::
    
        >>> rcv1.data.shape
        (804414, 47236)
    
    ``target``:
    The target values are stored in a scipy CSR sparse matrix, with 804414 samples 
    and 103 categories. Each sample has a value of 1 in its categories, and 0 in 
    others. The array has 3.15% of non zero values::
    
        >>> rcv1.target.shape
        (804414, 103)
    
    ``sample_id``:
    Each sample can be identified by its ID, ranging (with gaps) from 2286 
    to 810596::
    
        >>> rcv1.sample_id[:3]
        array([2286, 2287, 2288], dtype=uint32)
    
    ``target_names``:
    The target values are the topics of each sample. Each sample belongs to at 
    least one topic, and to up to 17 topics. There are 103 topics, each 
    represented by a string. Their corpus frequencies span five orders of 
    magnitude, from 5 occurrences for 'GMIL', to 381327 for 'CCAT'::
    
        >>> rcv1.target_names[:3].tolist()  # doctest: +SKIP
        ['E11', 'ECAT', 'M11']
    
    The dataset will be downloaded from the `rcv1 homepage`_ if necessary.
    The compressed size is about 656 MB.
    
    .. _rcv1 homepage: http://jmlr.csail.mit.edu/papers/volume5/lewis04a/
    
    
    .. topic:: References
    
        .. [1] Lewis, D. D., Yang, Y., Rose, T. G., & Li, F. (2004). 
               RCV1: A new benchmark collection for text categorization research. 
               The Journal of Machine Learning Research, 5, 361-397.
    

    因此,如果您想坚持原来的训练和测试子集,如上所述,您应该这样做:

    X_train = rcv1.data[0:23149,]
    X.train.shape
    # (23149, 47236)
    
    X_test = rcv1.data[23149:,]
    X_test.shape
    # (781265, 47236)
    

    对于您的y_trainy_test,同样使用rcv1.target

    如果您想使用不同的训练和测试分区,请使用:

    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(
            rcv1.data, rcv1.target, test_size=0.33, random_state=42)
    

    相应地调整您的test_size

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-23
      • 2020-02-06
      • 2016-07-13
      • 1970-01-01
      • 2015-04-14
      • 2013-04-21
      • 2021-05-10
      • 2022-06-18
      相关资源
      最近更新 更多