-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogistic.py
More file actions
39 lines (31 loc) · 1.41 KB
/
Copy pathlogistic.py
File metadata and controls
39 lines (31 loc) · 1.41 KB
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
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import DictVectorizer
from utils import *
def main():
train_data = load_data('./data/train.txt','dictionary')
train_set, test_set = train_test_split(train_data, test_size=0.2)
X_train = extract_features(train_set)
y_train = extract_labels(train_set)
X_test = extract_features(test_set)
y_test = extract_labels(test_set)
# Vectorize features
# converting the features to numerical vectors
vectorizer = DictVectorizer()
t1=[feature for sentence in X_train for feature in sentence]
t1.extend([feature for sentence in X_test for feature in sentence])
vectorizer = vectorizer.fit(t1)
X_train_vec = vectorizer.transform([feature for sentence in X_train for feature in sentence])
# vectorizer already fit
X_test_vec = vectorizer.transform([feature for sentence in X_test for feature in sentence])
# Train model
clf = LogisticRegression(max_iter=10000)
clf.fit(X_train_vec, [label for sentence in y_train for label in sentence])
# Test model
y_pred = clf.predict(X_test_vec)
# Evaluate model
accuracy = accuracy_score([label for sentence in y_test for label in sentence], y_pred)
print("Accuracy: {:.2f}%".format(accuracy * 100))
if __name__ == "__main__":
main()