我想将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']

10-07 16:30