File size: 1,445 Bytes
7f50cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Offline prediction through run.sh DATA_DIR OUTPUT_DIR."""

import csv
import json
from pathlib import Path
import sys

import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer

from text_features import paper_text


def main():
    data, output = map(Path, sys.argv[1:])
    method = Path(__file__).resolve().parent
    with (method / "vocabulary.json").open(encoding="utf-8") as stream:
        vocabulary = json.load(stream)
    with (data / "documents.jsonl").open(encoding="utf-8") as stream:
        documents = [json.loads(line) for line in stream if line.strip()]
    with np.load(method / "weights.npz", allow_pickle=False) as state:
        vectorizer = TfidfVectorizer(vocabulary=vocabulary)
        vectorizer.idf_ = state["idf"]
        features = vectorizer.transform([paper_text(row) for row in documents])
        predictions = np.asarray(features @ state["coef"]).ravel() + state["intercept"][0]
    if not np.isfinite(predictions).all():
        raise ValueError("Nonfinite prediction")
    output.mkdir(parents=True, exist_ok=True)
    with (output / "predictions.csv").open("w", encoding="utf-8", newline="") as stream:
        writer = csv.writer(stream)
        writer.writerow(["paper_id", "prediction"])
        writer.writerows((row["doc_id"], format(float(prediction), ".17g"))
                         for row, prediction in zip(documents, predictions))


if __name__ == "__main__":
    main()