LoginSignup
2
4

More than 5 years have passed since last update.

dictをarrayに変換

Posted at

特徴量をdictで持っている時に、scipyとかscikit-learnで使うようなarray形式のベクトルにしたい時

from sklearn.feature_extraction import DictVectorizer

features = [{"poko":2, "hoge":1}, {"hoge":2, "pokopoko":10}, {"poko":5, "hogehoge":1}]
vec = DictVectorizer()
array_vectors = vec.fit_transform(features).toarray()

でいける。特徴量のラベルを見たい時は

vec.get_feature_names()

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4