predict.py 2.64 KB
Newer Older
Nishtha Jain's avatar
Nishtha Jain committed
1
2
3
from model import svm_predict, load_model
from preprocessing import embedding_transform

Nishtha Jain's avatar
Nishtha Jain committed
4
def predict_one(X_test, embedding, class_group='medical', sampling='balanced', test_size=0.2, masking=True):
Nishtha Jain's avatar
Nishtha Jain committed
5
6
7
8
9
    X_test_embedded = embedding_transform(X_test, embedding, class_group, sampling, test_size, masking)
    svm_trained_model = load_model('svm', embedding, class_group, sampling, test_size, masking)
    prediction = svm_trained_model.predict(X_test_embedded)
    return prediction

Nishtha Jain's avatar
Nishtha Jain committed
10
def predict_all_models(X_test):
Nishtha Jain's avatar
Nishtha Jain committed
11
12
13
    
    for embedding in ['cv','self_w2v','w2v']:
        for sampling in ['random','balanced']:
Nishtha Jain's avatar
Nishtha Jain committed
14
            for masking in [True,False]:
Nishtha Jain's avatar
Nishtha Jain committed
15
16
17
18
19
20
21
22
                pred = predict_one(X_test, embedding=embedding, sampling=sampling, masking=masking)
    
                print("\nembedding :",embedding)
                print("sampling :",sampling)
                print("masking :",masking)
                for i,x in enumerate(X_test):
                    print("sent :",x)
                    print("pred :",pred[i])
Nishtha Jain's avatar
Nishtha Jain committed
23
#                    print()
Nishtha Jain's avatar
Nishtha Jain committed
24

Nishtha Jain's avatar
Nishtha Jain committed
25
26
27
    for sampling in ['random','balanced']:
        pred = predict_one(X_test, embedding='elmo', sampling=sampling, masking=True)
    
Nishtha Jain's avatar
Nishtha Jain committed
28
29
30
31
32
33
        print("\nembedding :",embedding)
        print("sampling :",sampling)
        print("masking :",masking)
        for i,x in enumerate(X_test):
            print("sent :",x)
            print("pred :",pred[i])
Nishtha Jain's avatar
Nishtha Jain committed
34
#            print()
Nishtha Jain's avatar
Nishtha Jain committed
35

Nishtha Jain's avatar
Nishtha Jain committed
36
if __name__ == "__main__":
Nishtha Jain's avatar
Nishtha Jain committed
37
38
    import argparse
    parser = argparse.ArgumentParser()
Nishtha Jain's avatar
Nishtha Jain committed
39

Nishtha Jain's avatar
Nishtha Jain committed
40
    parser.add_argument("--masking", dest='masking',action='store_true', help = "for 'bio' data")
Nishtha Jain's avatar
Nishtha Jain committed
41
42
    parser.add_argument("--no-masking", dest='masking',action='store_false', help = "for 'raw' data")
    
Nishtha Jain's avatar
Nishtha Jain committed
43
44
45
    parser.add_argument("--sampling", help = "choice of sampling ('random', 'balanced')")
    parser.add_argument("--embedding", help = "choice of embeddings to be used ('cv': count_vectorize(self-trained), 'w2v': word2vec_embedding(pre-trained), 'self_w2v':w2v(self-trained), 'elmo':elmo(pre-trained))")
    parser.add_argument("--pred_all_models", help = 'yes or no')
Nishtha Jain's avatar
Nishtha Jain committed
46
47
48

    args = parser.parse_args()
    X_test = ['She works at the hospital','He works at the hospital']
Nishtha Jain's avatar
Nishtha Jain committed
49
50
51
    if args.pred_all_models == 'yes':
        predict_all_models(X_test)
    else:
Nishtha Jain's avatar
Nishtha Jain committed
52

Nishtha Jain's avatar
Nishtha Jain committed
53
54
55
56
57
58
59
60
        pred = predict_one(X_test, embedding=args.embedding, sampling=args.sampling, masking=args.masking)
        
        print("embedding :",args.embedding)
        print("sampling :",args.sampling)
        print("masking :",args.masking)
        for i,x in enumerate(X_test):
            print("sent :",x)
            print("pred :",pred[i])
Nishtha Jain's avatar
Nishtha Jain committed
61
#            print()