-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanalysis_lr.py
executable file
·72 lines (56 loc) · 1.89 KB
/
analysis_lr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import pickle
import argparse
import numpy as np
def analysis_lr(args):
'''Print the high importance features used by the linear regression model'''
num_features = args.topk_features
ignore_idf = args.ignore_idf
# load model
with open(args.model_path, 'rb') as fr:
model = pickle.load(fr)
vect = model.get_params()['vect']
tfidf = model.get_params()['tfidf']
clf = model.get_params()['clf']
vocab = vect.vocabulary_
idfs = tfidf.idf_
clf_weights = clf.coef_
rev_vocab = {idx: tok for tok, idx in vocab.items()}
label_dim, vocab_size = clf_weights.shape
assert(label_dim == 1) #Otherwise, you shouldn't flatten it
flat_weights = clf_weights.flatten()
print(ignore_idf)
if not ignore_idf:
flat_weights = flat_weights * idfs
inc_idx = np.argsort(flat_weights)
pos_idx = inc_idx[-num_features:][::-1]
neg_idx = inc_idx[:num_features]
print("POSTIVE FEATURES")
for i in range(num_features):
print(f"FEATURE #{i}:\tTOKEN:{rev_vocab[pos_idx[i]]},\tWEIGHT:{flat_weights[pos_idx[i]]}")
print("NEGATIVE FEATURES")
for i in range(num_features):
print(f"FEATURE #{i}:\tTOKEN:{rev_vocab[neg_idx[i]]},\tWEIGHT:{flat_weights[neg_idx[i]]}")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--model_path',
default=None,
type=str,
required=True,
help='The model pickle file'
)
parser.add_argument(
'--topk_features',
default=5,
type=int,
help='how many top features to print'
)
parser.add_argument(
'--ignore_idf',
action="store_true",
help='if set True, ignore the influence of the idf step'
)
args = parser.parse_args()
analysis_lr(args)