代码之家  ›  专栏  ›  技术社区  ›  Lin Ma

在scikit learn GradientBoostingClassifier中设置序列错误的数组元素

  •  1
  • Lin Ma  · 技术社区  · 7 年前

    这是我的密码,有人知道怎么了吗?我打电话的时候出错了 fit

    import pandas as pd
    import numpy as np
    from sklearn.ensemble import (RandomTreesEmbedding, RandomForestClassifier,
                                  GradientBoostingClassifier)
    from sklearn.model_selection import train_test_split
    from sklearn.feature_extraction.text import CountVectorizer
    
    n_estimators = 10
    d = {'f1': [1, 2], 'f2': ['foo goo', 'goo zoo'], 'target':[0, 1]}
    df = pd.DataFrame(data=d)
    X_train, X_test, y_train, y_test = train_test_split(df, df['target'], test_size=0.1)
    
    X_train['f2'] = CountVectorizer().fit_transform(X_train['f2'])
    X_test['f2'] = CountVectorizer().fit_transform(X_test['f2'])
    
    grd = GradientBoostingClassifier(n_estimators=n_estimator, max_depth=10)
    grd.fit(X_train.values, y_train.values)
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Lucas Piyush S. Wanare    7 年前

    问题在于 CountVectorizer :

    import pandas as pd
    from sklearn.feature_extraction.text import CountVectorizer
    
    d = {'f1': [1, 2], 'f2': ['foo goo', 'goo zoo'], 'target':[0, 1]}
    df = pd.DataFrame(data=d)
    df['f2'] = CountVectorizer().fit_transform(df['f2'])
    

    df.values 是:

    array([[1,
            <2x3 sparse matrix of type '<class 'numpy.int64'>'
        with 4 stored elements in Compressed Sparse Row format>,
            0],
           [2,
            <2x3 sparse matrix of type '<class 'numpy.int64'>'
        with 4 stored elements in Compressed Sparse Row format>,
            1]], dtype=object)
    

    todense()

    dense_count = CountVectorizer().fit_transform(df['f2']).todense()
    

    哪里 dense_count 是这样的:

    matrix([[1, 1, 0],
            [0, 1, 1]], dtype=int64)