-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproba.py
More file actions
134 lines (103 loc) · 3.54 KB
/
Copy pathproba.py
File metadata and controls
134 lines (103 loc) · 3.54 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# Author: Angela Chapman
# Date: 8/6/2014
#
# This file contains code to accompany the Kaggle tutorial
# "Deep learning goes to the movies". The code in this file
# is for Part 1 of the tutorial on Natural Language Processing.
#
# *************************************** #
import os
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier
from KaggleWord2VecUtility import KaggleWord2VecUtility
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import cross_validation
from sklearn.datasets import make_multilabel_classification
import pandas as pd
import numpy as np
import json
import sys
import time
from collections import defaultdict
from sklearn.cluster import KMeans
from numpy.random import RandomState
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier
rng = RandomState(42)
from sklearn.decomposition import PCA
from sklearn.decomposition import SparsePCA
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import f1_score
if __name__ == '__main__':
input1=sys.argv[1] # train data
input2=sys.argv[2] # test data
tagdic=sys.argv[3] # dictionary for tag 2000
train=json.load(open(input1))
test=json.load(open(input2))
tag_dic=json.load(open(tagdic))
# Initialize an empty list to hold the clean reviews
traindata = []
testdata = []
Y1=[]
Y2=[]
# Loop over each review; create an index i that goes from 0 to the length
# of the movie review list
for i in train:
buf=[]
traindata.append(" ".join(KaggleWord2VecUtility.review_to_wordlist(train[i][0]+train[i][1], True)))
for j in train[i][3].split():
if j in tag_dic:
buf.append(tag_dic[j])
Y1.append(buf)
for i in test:
buf=[]
testdata.append(" ".join(KaggleWord2VecUtility.review_to_wordlist(test[i][0]+test[i][1], True)))
for j in test[i][3].split():
if j in tag_dic:
buf.append(tag_dic[j])
Y2.append(buf)
# ****** Create a bag of words from the training set
#
# Initialize the "CountVectorizer" object, which is scikit-learn's
# Tfidf tool.
vectorizer = CountVectorizer(min_df=0.001)
X_all=traindata+testdata
lentrain=len(traindata)
X = vectorizer.fit_transform(X_all)
X_train = X[:lentrain]
X_test = X[lentrain:]
X1 = X_train.toarray()
X2 = X_test.toarray()
# X1 = X_train
# X2 = X_test
# clf = GaussianNB()
# clf=SGDClassifier()
clf=LinearSVC(random_state=0)
# clf=RandomForestClassifier(n_estimators = 100)
# clf=MultinomialNB()
classif = OneVsRestClassifier(clf).fit(X1, Y1)
class_set=classif.classes_
scores=classif.decision_function(X2)
Y3=[]
# predict=classif.predict(X2)
if len(scores.shape) == 1:
indices = (scores > 0).astype(np.int)
else:
for score in scores:
buf=[]
for i in range(9):
if score[i]>0:
buf.append(class_set[i])
if not buf:
indices = np.argmax(score)
buf.append(class_set[indices])
Y3.append(buf)
for i in Y3:
print(i)
print(f1_score(Y3,Y2))