-
Notifications
You must be signed in to change notification settings - Fork 0
/
snli.py
executable file
·202 lines (115 loc) · 4.19 KB
/
snli.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# coding: utf-8
# In[1]:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.model_selection import train_test_split # function for splitting data to train and test sets
import nltk
from nltk.corpus import stopwords
from nltk.classify import SklearnClassifier
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')
import sklearn
from sklearn.naive_bayes import MultinomialNB
from scipy import sparse
# In[2]:
snli_train = pd.read_csv('data/snli-train.csv')
snli_train = snli_train[['sentence1','sentence2','label']]
# In[3]:
snli_dev = pd.read_csv('data/snli-train.csv')
snli_dev = snli_dev[['sentence1','sentence2','label']]
# In[4]:
snli_test = pd.read_csv('data/snli-test.csv',error_bad_lines=False)
snli_test = snli_test[['sentence1','sentence2']]
# In[5]:
from nltk.stem import PorterStemmer
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.stem.wordnet import WordNetLemmatizer
# In[6]:
#preprocessing Train data
import re
ps = PorterStemmer()
stops = set(stopwords.words("english"))
clean_train_sentence1=[]
clean_train_sentence2=[]
for review in snli_train["sentence1"]:
review = re.sub(r"[^a-zA-Z]"," ", review)
words = review.lower().split()
words = [w for w in words if not w in stops]
#words = [ps.stem(w) for w in words ]
clean_train_sentence1.append(" ".join(words))
for review in snli_train["sentence2"]:
review = re.sub(r"[^a-zA-Z]"," ", str(review))
words = review.lower().split()
words = [w for w in words if not w in stops]
#words = [ps.stem(w) for w in words ]
clean_train_sentence2.append(" ".join(words))
# In[7]:
len(snli_train["sentence1"])
# In[8]:
#preprocessing test data
clean_test_sentence1=[]
clean_test_sentence2=[]
for review in snli_test["sentence1"]:
review = re.sub("[^a-zA-Z]"," ", review)
words = review.lower().split()
words = [w for w in words if not w in stops]
#words = [ps.stem(w) for w in words ]
clean_test_sentence1.append(" ".join(words))
for review in snli_test["sentence2"]:
review = re.sub("[^a-zA-Z]"," ", review)
words = review.lower().split()
words = [w for w in words if not w in stops]
#words = [ps.stem(w) for w in words ]
clean_test_sentence2.append(" ".join(words))
# In[9]:
#preprocessing dev data
clean_dev_sentence1=[]
clean_dev_sentence2=[]
for review in snli_dev["sentence1"]:
review = re.sub("[^a-zA-Z]"," ", review)
words = review.lower().split()
words = [w for w in words if not w in stops]
#words = [ps.stem(w) for w in words ]
clean_dev_sentence1.append(" ".join(words))
for review in snli_dev["sentence2"]:
review = re.sub("[^a-zA-Z]"," ", str(review))
words = review.lower().split()
words = [w for w in words if not w in stops]
#words = [ps.stem(w) for w in words ]
clean_dev_sentence2.append(" ".join(words))
# In[10]:
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
count_vec1 = TfidfVectorizer(stop_words='english')
count_vec2 = TfidfVectorizer(stop_words='english')
# In[11]:
train_count2=count_vec2.fit_transform(clean_train_sentence2)
train_count1=count_vec1.fit_transform(clean_train_sentence1)
train_count=sparse.hstack((train_count1,train_count2))
# In[12]:
train_count2
# In[13]:
train_count1
# In[14]:
test_count2=count_vec2.transform(clean_test_sentence2)
test_count1=count_vec1.transform(clean_test_sentence1)
test_count=sparse.hstack((test_count1,test_count2))
# In[15]:
dev_count2=count_vec2.transform(clean_dev_sentence2)
dev_count1=count_vec1.transform(clean_dev_sentence2)
dev_count=sparse.hstack((dev_count1,dev_count2))
# In[16]:
from sklearn.cross_validation import KFold,cross_val_score
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB()
scores=cross_val_score(clf,train_count,snli_train["label"],cv=5,scoring='accuracy')
scores.mean()
# In[17]:
clf1 = MultinomialNB().fit(train_count,snli_train["label"])
y_pred = clf1.predict(test_count)
y_pred = pd.DataFrame(y_pred,columns=['label'])
frames=[snli_test,y_pred]
result=pd.concat(frames,axis=1)
result.to_csv("snli-test.csv", sep='\t')
# In[18]:
y_pred_dev = clf1.predict(dev_count)