Skip to content

Commit

Permalink
Work-around to display weights for models using sparse coef_
Browse files Browse the repository at this point in the history
  • Loading branch information
cgpotts committed Apr 29, 2019
1 parent 4201cf0 commit f6a5f11
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rel_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ def examine_model_weights(train_result, k=3, verbose=True):
feature_names = train_result['vectorizer'].get_feature_names()
for rel, model in train_result['models'].items():
print('Highest and lowest feature weights for relation {}:\n'.format(rel))
sorted_weights = sorted([(wgt, idx) for idx, wgt in enumerate(model.coef_[0])], reverse=True)
try:
coefs = model.coef_.toarray()
except AttributeError:
coefs = model.coef_
sorted_weights = sorted([(wgt, idx) for idx, wgt in enumerate(coefs[0])], reverse=True)
for wgt, idx in sorted_weights[:k]:
print('{:10.3f} {}'.format(wgt, feature_names[idx]))
print('{:>10s} {}'.format('.....', '.....'))
Expand Down

0 comments on commit f6a5f11

Please sign in to comment.