-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinference.py
63 lines (40 loc) · 1.64 KB
/
inference.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
import pandas as pd
import pickle
import joblib
from tqdm import tqdm
from polarity import detect_sentiment, detect_subjectivity
def apply_gender(df):
df['gender'] = df['text'].progress_apply(lambda text: gender_model.predict([text])[0])
df.to_pickle('data/inferences/tweets-with-gender.pkl')
def apply_political(df):
df['political'] = df['text'].progress_apply(lambda text: political_model.predict([text])[0])
df.to_pickle('data/inferences/tweets-with-political.pkl')
def apply_age(df):
df['age'] = df['text'].progress_apply(lambda text: age_model.predict([text])[0])
df.to_pickle('data/inferences/tweets-with-age.pkl')
def apply_sentiment(df):
df = detect_sentiment(df)
df.to_pickle('data/inferences/tweets-with-sentiment.pkl')
def apply_subjectivity(df):
df = detect_subjectivity(df)
df.to_pickle('data/inferences/tweets-with-subj.pkl')
if __name__ == "__main__":
tqdm.pandas()
df = pd.read_pickle('data/tweets-us-all.pkl')
# Load the 3 models
with open('pretrained-models/gender/clf.pkl', 'rb') as f:
gender_model = pickle.load(f)
with open('pretrained-models/political-orientation/model.pkl', 'rb') as f:
political_model = pickle.load(f)
with open('pretrained-models/user-age/text_age_pipeline.pkl', 'rb') as f:
age_model = pickle.load(f)
print("Political orientation predictions...")
apply_political(df)
print("Gender predictions...")
apply_gender(df)
print("Sentiment predictions...")
apply_sentiment(df)
print("Subjectivity predictions...")
apply_subjectivity(df)
print("Age predictions...")
apply_age(df)