将 Python 字典列表转换为 SciPy 稀疏矩阵

Converting a list of Python dictionaries into a SciPy sparse matrix

我想将 Python 字典列表转换为 SciPy 稀疏矩阵。

我知道我可以使用 sklearn.feature_extraction.DictVectorizer.fit_transform():

import sklearn.feature_extraction
feature_dictionary = [{"feat1": 1.5, "feat10": 0.5}, 
                      {"feat4": 2.1, "feat5": 0.3, "feat7": 0.1}, 
                      {"feat2": 7.5}]

v = sklearn.feature_extraction.DictVectorizer(sparse=True, dtype=float)
X = v.fit_transform(feature_dictionary)
print('X: \n{0}'.format(X))

输出:

X: 
  (0, 0)    1.5
  (0, 1)    0.5
  (1, 3)    2.1
  (1, 4)    0.3
  (1, 5)    0.1
  (2, 2)    7.5

但是,我希望 feat1 在第 1 列,feat10 在第 10 列,feat4 在第 4 列,依此类推。我怎样才能做到这一点?

您可以手动设置 sklearn.feature_extraction.DictVectorizer.vocabulary_sklearn.feature_extraction.DictVectorizer.fit.feature_names_ 而不是通过 sklearn.feature_extraction.DictVectorizer.fit() 学习它们:

import sklearn.feature_extraction
feature_dictionary = [{"feat1": 1.5, "feat10": 0.5}, {"feat4": 2.1, "feat5": 0.3, "feat7": 0.1}, {"feat2": 7.5}]

v = sklearn.feature_extraction.DictVectorizer(sparse=True, dtype=float)
v.vocabulary_ = {'feat0': 0, 'feat1': 1, 'feat2': 2, 'feat3': 3, 'feat4': 4, 'feat5': 5, 
                 'feat6': 6,  'feat7': 7, 'feat8': 8, 'feat9': 9, 'feat10': 10}
v.feature_names_ = ['feat0', 'feat1', 'feat2', 'feat3', 'feat4', 'feat5', 'feat6', 'feat7', 
                    'feat8', 'feat9', 'feat10']

X = v.transform(feature_dictionary)
print('v.vocabulary_ : {0} ; v.feature_names_: {1}'.format(v.vocabulary_, v.feature_names_))
print('X: \n{0}'.format(X))

输出:

X: 
  (0, 1)    1.5
  (0, 10)   0.5
  (1, 4)    2.1
  (1, 5)    0.3
  (1, 7)    0.1
  (2, 2)    7.5

显然您不必手动定义 vocabulary_feature_names_

v.vocabulary_ = {}
v.feature_names_ = []
number_of_features = 11
for feature_number in range(number_of_features):
    feature_name = 'feat{0}'.format(feature_number) 
    v.vocabulary_[feature_name] = feature_number
    v.feature_names_.append(feature_name)                                      

print('v.vocabulary_ : {0} ; v.feature_names_: {1}'.format(v.vocabulary_, v.feature_names_))   

输出:

v.vocabulary_ : {'feat10': 10, 'feat9': 9, 'feat8': 8, 'feat5': 5, 'feat4': 4, 'feat7': 7, 
                 'feat6': 6, 'feat1': 1, 'feat0': 0, 'feat3': 3, 'feat2': 2}
v.feature_names_: ['feat0', 'feat1', 'feat2', 'feat3', 'feat4', 'feat5', 'feat6', 'feat7', 
                   'feat8', 'feat9', 'feat10']