-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_odds_ratios.py
34 lines (26 loc) · 1.04 KB
/
get_odds_ratios.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
from controversy_pipeline import ContentiousVectorizer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import sys
import pandas as pd
import numpy as np
vectorizer = ContentiousVectorizer()
scaler = StandardScaler()
model = LogisticRegression(solver='liblinear', multi_class='auto', max_iter=1000)
pipeline = Pipeline([("vectorizer", vectorizer), ("scaler", scaler), ("model", model)])
X = pd.read_pickle(sys.argv[1])
y = X.label
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
pipeline.fit(X_train, y_train)
print(model.coef_.shape)
print(vectorizer.get_labels())
coef_df = pd.DataFrame()
coef_df["label"] = vectorizer.get_labels()
print(np.exp(model.coef_).shape)
coef_df["odds_ratio"] = np.exp(model.coef_[0])
print(coef_df)
coef_df = coef_df[coef_df.label != 'Text']
coef_df = coef_df.sort_values(by='odds_ratio', ascending=False)
coef_df.to_csv(sys.argv[2] + "_coef.csv", sep=";")