-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvc_linear.py
219 lines (170 loc) · 6.94 KB
/
svc_linear.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 23 11:28:21 2023
@author: isabelbeaulieu
"""
import pandas as pd
import re
import nltk
import os
nltk.download('wordnet')
nltk.download('punkt')
nltk.download('stopwords')
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
path= '/Users/isabelbeaulieu/Desktop/Data Mining'
os.chdir(path)
train = pd.read_csv("train.csv")
train['dataset'] = 'train'
test = pd.read_csv("test.csv")
test['dataset'] = 'test'
val = pd.read_csv("val.csv")
val['dataset'] = 'val'
df = pd.concat([train, test, val], ignore_index=True, axis=0)
def clean_text_remove_stop(df):
sentences = []
for i in range(0,len(df)):
sent=df["sentence"][i]
sent=re.sub(r'[,.;@#?!&$\-\']+', ' ', sent, flags=re.IGNORECASE)
sent=re.sub(' +', ' ', sent, flags=re.IGNORECASE)
sent=re.sub(r'\"', ' ', sent, flags=re.IGNORECASE)
sent=re.sub(r'[^a-zA-Z]', " ", sent, flags=re.VERBOSE)
sent=sent.replace(',', '')
sent=' '.join(sent.split())
sent=re.sub("\n|\r", "", sent)
sent = ' '.join([word for word in sent.split() if word not in stopwords.words("english")])
sentences.append(sent)
df['clean'] = sentences
return df
def clean_text_keep_stop(df):
sentences = []
for i in range(0,len(df)):
sent=df["sentence"][i]
sent=re.sub(r'[,.;@#?!&$\-\']+', ' ', sent, flags=re.IGNORECASE)
sent=re.sub(' +', ' ', sent, flags=re.IGNORECASE)
sent=re.sub(r'\"', ' ', sent, flags=re.IGNORECASE)
sent=re.sub(r'[^a-zA-Z]', " ", sent, flags=re.VERBOSE)
sent=sent.replace(',', '')
sent=' '.join(sent.split())
sent=re.sub("\n|\r", "", sent)
sentences.append(sent)
df['clean'] = sentences
return df
def CountVect(df):
sent_list=[]
for i in range(0,len(df)):
sent_list.append(df['clean'][i])
MyCountV=CountVectorizer(
input="content",
lowercase=True)
MyDTM = MyCountV.fit_transform(sent_list) # create a sparse matrix
MyDTM = MyDTM.toarray() # convert to a regular array
ColumnNames=MyCountV.get_feature_names_out()
MyDTM_DF=pd.DataFrame(MyDTM,columns=ColumnNames)
return(MyDTM_DF)
def tfidf(df):
sent_list=[]
for i in range(0,len(df)):
sent_list.append(df['clean'][i])
MyVect_TF=TfidfVectorizer(input='content')
Vect = MyVect_TF.fit_transform(sent_list)
ColumnNamesTF=MyVect_TF.get_feature_names_out()
DF_TF=pd.DataFrame(Vect.toarray(),columns=ColumnNamesTF)
return (DF_TF)
''' Here is an example of how to use the code above.
Say you want to build a model with the input using tf-idf vectorizer and keeping
stopwords. After running the code above, this is what you would run.'''
##keeping stopwords - tfidf
clean = clean_text_keep_stop(df)
tf_matrix = tfidf(df)
train_clean = clean[clean['dataset'] == 'train']
train_index = clean[clean['dataset'] == 'train'].index.values.astype(int)
test_clean = clean[clean['dataset'] == 'test']
test_index = clean[clean['dataset'] == 'test'].index.values.astype(int)
val_clean = clean[clean['dataset'] == 'val']
val_index = clean[clean['dataset'] == 'val'].index.values.astype(int)
trainLabel = train_clean['emotion'].astype('category')
testLabel = test_clean['emotion'].astype('category')
valLabel = val_clean['emotion'].astype('category')
train_df = tf_matrix.iloc[train_index]
test_df = tf_matrix.iloc[test_index]
val_df = tf_matrix.iloc[val_index]
from sklearn.svm import SVC, LinearSVC
from sklearn import metrics
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix,classification_report
model = LinearSVC()
model.fit(train_df, trainLabel)
pred = model.predict(test_df)
acc_score = metrics.accuracy_score(pred,testLabel)
prec_score = precision_score(testLabel,pred, average='macro')
recall = recall_score(testLabel, pred,average='macro')
f1 = f1_score(testLabel,pred,average='macro')
matrix = confusion_matrix(testLabel,pred)
print(str('Accuracy: '+'{:04.2f}'.format(acc_score*100))+'%')
print(str('Precision: '+'{:04.2f}'.format(prec_score*100))+'%')
print(str('Recall: '+'{:04.2f}'.format(recall*100))+'%')
print('F1 Score: ',f1)
print('\n')
print(classification_report(testLabel,pred))
###loooking at removing stopwords - tfidf
clean = clean_text_remove_stop(df)
tf_matrix = tfidf(df)
train_clean = clean[clean['dataset'] == 'train']
train_index = clean[clean['dataset'] == 'train'].index.values.astype(int)
test_clean = clean[clean['dataset'] == 'test']
test_index = clean[clean['dataset'] == 'test'].index.values.astype(int)
val_clean = clean[clean['dataset'] == 'val']
val_index = clean[clean['dataset'] == 'val'].index.values.astype(int)
trainLabel = train_clean['emotion'].astype('category')
testLabel = test_clean['emotion'].astype('category')
valLabel = val_clean['emotion'].astype('category')
train_df = tf_matrix.iloc[train_index]
test_df = tf_matrix.iloc[test_index]
val_df = tf_matrix.iloc[val_index]
model = LinearSVC()
model.fit(train_df, trainLabel)
pred = model.predict(test_df)
acc_score = metrics.accuracy_score(pred,testLabel)
prec_score = precision_score(testLabel,pred, average='macro')
recall = recall_score(testLabel, pred,average='macro')
f1 = f1_score(testLabel,pred,average='macro')
matrix = confusion_matrix(testLabel,pred)
print(str('Accuracy: '+'{:04.2f}'.format(acc_score*100))+'%')
print(str('Precision: '+'{:04.2f}'.format(prec_score*100))+'%')
print(str('Recall: '+'{:04.2f}'.format(recall*100))+'%')
print('F1 Score: ',f1)
print('\n')
print(classification_report(testLabel,pred))
###count vect and remove stop
clean = clean_text_remove_stop(df)
cv_matrix = CountVect(df)
train_clean = clean[clean['dataset'] == 'train']
train_index = clean[clean['dataset'] == 'train'].index.values.astype(int)
test_clean = clean[clean['dataset'] == 'test']
test_index = clean[clean['dataset'] == 'test'].index.values.astype(int)
val_clean = clean[clean['dataset'] == 'val']
val_index = clean[clean['dataset'] == 'val'].index.values.astype(int)
trainLabel = train_clean['emotion'].astype('category')
testLabel = test_clean['emotion'].astype('category')
valLabel = val_clean['emotion'].astype('category')
train_df = cv_matrix.iloc[train_index]
test_df = cv_matrix.iloc[test_index]
val_df = cv_matrix.iloc[val_index]
model = LinearSVC()
model.fit(train_df, trainLabel)
pred = model.predict(test_df)
acc_score = metrics.accuracy_score(pred,testLabel)
prec_score = precision_score(testLabel,pred, average='macro')
recall = recall_score(testLabel, pred,average='macro')
f1 = f1_score(testLabel,pred,average='macro')
matrix = confusion_matrix(testLabel,pred)
print(str('Accuracy: '+'{:04.2f}'.format(acc_score*100))+'%')
print(str('Precision: '+'{:04.2f}'.format(prec_score*100))+'%')
print(str('Recall: '+'{:04.2f}'.format(recall*100))+'%')
print('F1 Score: ',f1)
print('\n')
print(classification_report(testLabel,pred))