Skip to content

Commit db2fe58

Browse files
author
heikofriedrich
committed
Merge pull request #17 from FedericoMarroni/master
multiclass classifier
2 parents 1d13c3c + a1929e3 commit db2fe58

2 files changed

Lines changed: 241 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
__author__ = 'Federico'
2+
# Multiclass Naive-Bayes classifier for categorization of WoN e-mail dataset
3+
# It uses MultinomialNB classifier
4+
5+
from numpy import *
6+
from tools.tensor_utils import read_input_tensor, SparseTensor
7+
from sklearn import metrics
8+
from sklearn.naive_bayes import MultinomialNB
9+
from sklearn.pipeline import Pipeline
10+
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
11+
from nltk.corpus import stopwords
12+
13+
# Get the input from a folder in C:
14+
def get_example_data():
15+
16+
header_file = 'C:/Users/Federico/Desktop/test/evaluation/tensor_content_NEW/headers.txt'
17+
data_file_prefix = 'C:/Users/Federico/Desktop/test/evaluation/tensor_content_NEW'
18+
data_files = [data_file_prefix + "/connection.mtx",
19+
data_file_prefix + "/needtype.mtx",
20+
data_file_prefix + "/subject.mtx",
21+
data_file_prefix + "/content.mtx",
22+
data_file_prefix + "/category.mtx"]
23+
slices = [SparseTensor.CONNECTION_SLICE, SparseTensor.NEED_TYPE_SLICE, SparseTensor.ATTR_SUBJECT_SLICE,
24+
SparseTensor.ATTR_CONTENT_SLICE, SparseTensor.CATEGORY_SLICE]
25+
26+
tensor = read_input_tensor(header_file, data_files, slices, False)
27+
28+
data = []
29+
target = []
30+
31+
# Store the chosen input into lists.
32+
# The "if" statement is meant to include only samples with a single category (No multilabel)
33+
for need_index in tensor.getNeedIndices():
34+
content = ""
35+
categories = tensor.getAttributesForNeed(need_index, SparseTensor.CATEGORY_SLICE)
36+
numCategories = len(categories)
37+
if numCategories >= 1:
38+
category_index = tensor.getSliceMatrix(SparseTensor.CATEGORY_SLICE)[need_index,].nonzero()[1][0]
39+
target.append(category_index)
40+
for word in tensor.getAttributesForNeed(need_index, SparseTensor.ATTR_SUBJECT_SLICE):
41+
content += word + " "
42+
data.append(content)
43+
44+
# Include only few of all the categories (e.g. with samples > n)
45+
newdata = []
46+
newtarget = []
47+
for i in range(len(target)):
48+
49+
if target.count(target[i]) > 50:
50+
newtarget.append(target[i])
51+
newdata.append(data[i])
52+
53+
data = newdata
54+
target = newtarget
55+
56+
# Print out the input, just a check:
57+
target_names = tensor.getHeaders()
58+
print("test")
59+
print data
60+
print target_names
61+
print target
62+
63+
return data, target, target_names
64+
65+
# Call for the input
66+
my_data, my_target, my_targetname = get_example_data()
67+
68+
# A little information about dimensions and format of the input:
69+
print type(my_data), type(my_target), # format of data and targets
70+
print len(my_data) # number of samples
71+
print len(my_target)
72+
73+
74+
# Let's build the training and testing datasets:
75+
SPLIT_PERC = 0.80 # 80% goes into training, 20% into test
76+
split_size = int(len(my_data)*SPLIT_PERC)
77+
X_train = my_data[:split_size]
78+
X_test = my_data[split_size:]
79+
y_train = my_target[:split_size]
80+
y_test = my_target[split_size:]
81+
82+
83+
# Training, prediction and evaluation of the classifier(s):
84+
def train_and_evaluate(clf, X_train, X_test, y_train, y_test, y_name):
85+
86+
# Training
87+
clf.fit(X_train, y_train)
88+
# Prediction of testing sets
89+
y_pred = clf.predict(X_test)
90+
91+
# Precision, recall and support (i.e. nr. of samples used for the testing)
92+
print "Classification Report:"
93+
print metrics.classification_report(y_test, y_pred)
94+
# Confusion Matrix
95+
print "Confusion Matrix:"
96+
print metrics.confusion_matrix(y_test, y_pred)
97+
98+
# Visualization of Categories / Assigned / Data
99+
print "Tested data => assigned category, data:"
100+
for i in range(len(X_test)):
101+
print str(i) + ") Real category: " + str(y_name[y_test[i]]) + ", Assigned category: " + \
102+
str(y_name[y_pred[i]]) + ", Data: " + str(X_test[i])
103+
104+
# Assign names to the categories (defined by numbers)
105+
print "\n Categories: \n"
106+
categories = set()
107+
for cat in y_pred:
108+
categories.add(cat)
109+
categories = sorted(categories)
110+
for cat in categories:
111+
print str(cat) + " " + y_name[cat]
112+
113+
# Introducing stop words
114+
stopset = set(stopwords.words('english'))
115+
116+
# Two different classifiers: Count and Tfidf vectors
117+
clf_count = Pipeline([
118+
('vect', CountVectorizer(
119+
stop_words=stopset,
120+
token_pattern=ur"\b[a-z0-9_\-\.]+[a-z][a-z0-9_\-\.]+\b",
121+
)),
122+
('clf', MultinomialNB(alpha=1)),
123+
])
124+
125+
clf_tfidf = Pipeline([
126+
('vect', TfidfVectorizer(
127+
stop_words=stopset,
128+
token_pattern=ur"\b[a-z0-9_\-\.]+[a-z][a-z0-9_\-\.]+\b",
129+
)),
130+
('clf', MultinomialNB(alpha=1)),
131+
])
132+
133+
# List of classifiers
134+
clfs = [clf_count, clf_tfidf]
135+
136+
# Run the evaluation/classification
137+
for clf in clfs:
138+
train_and_evaluate(clf, X_train, X_test, y_train, y_test, my_targetname)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
__author__ = 'Federico'
2+
# Multilabel (i.e. a sample is assigned to more than one category) Naive Bayes classifier for WoN dataset
3+
#It uses OneVsRest, MultinomialNB classification strategies
4+
5+
from numpy import *
6+
from tools.tensor_utils import read_input_tensor, SparseTensor
7+
from sklearn import metrics
8+
from sklearn.naive_bayes import MultinomialNB
9+
from sklearn.pipeline import Pipeline
10+
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
11+
from nltk.corpus import stopwords
12+
from sklearn.multiclass import OneVsRestClassifier
13+
14+
# Get the input from a folder in C:
15+
def get_example_data():
16+
17+
header_file = 'C:/Users/Federico/Desktop/test/evaluation/tensor_content_NEW/headers.txt'
18+
data_file_prefix = 'C:/Users/Federico/Desktop/test/evaluation/tensor_content_NEW'
19+
data_files = [data_file_prefix + "/connection.mtx",
20+
data_file_prefix + "/needtype.mtx",
21+
data_file_prefix + "/subject.mtx",
22+
data_file_prefix + "/content.mtx",
23+
data_file_prefix + "/category.mtx"]
24+
slices = [SparseTensor.CONNECTION_SLICE, SparseTensor.NEED_TYPE_SLICE, SparseTensor.ATTR_SUBJECT_SLICE,
25+
SparseTensor.ATTR_CONTENT_SLICE, SparseTensor.CATEGORY_SLICE]
26+
27+
tensor = read_input_tensor(header_file, data_files, slices, False)
28+
29+
data = []
30+
target = []
31+
32+
# Store the chosen input into lists.
33+
for need_index in tensor.getNeedIndices():
34+
content = ""
35+
category_index = tensor.getSliceMatrix(SparseTensor.CATEGORY_SLICE)[need_index,].nonzero()[1].tolist()
36+
target.append(category_index)
37+
for word in tensor.getAttributesForNeed(need_index, SparseTensor.ATTR_SUBJECT_SLICE):
38+
content += word + " "
39+
data.append(content)
40+
41+
# Print out the input, just a check:
42+
target_names = tensor.getHeaders()
43+
print("test")
44+
print data
45+
print target_names
46+
print target
47+
48+
return data, target, target_names
49+
50+
# Call for the input
51+
my_data, my_target, my_targetname = get_example_data()
52+
53+
# A little information about dimensions and format of the input:
54+
print type(my_data), type(my_target), # format of data and targets
55+
print len(my_data) # number of samples
56+
print len(my_target)
57+
58+
59+
# Let's build the training and testing datasets:
60+
SPLIT_PERC = 0.80 # 80% goes into training, 20% into test
61+
split_size = int(len(my_data)*SPLIT_PERC)
62+
X_train = my_data[:split_size]
63+
X_test = my_data[split_size:]
64+
y_train = my_target[:split_size]
65+
y_test = my_target[split_size:]
66+
67+
68+
# Training, prediction and evaluation of the classifier(s):
69+
def train_and_evaluate(clf, X_train, X_test, y_train, y_test, y_name):
70+
#Training and prediction
71+
clf.fit(X_train, y_train)
72+
y_pred = clf.predict(X_test)
73+
74+
# Precision, recall and support (i.e. nr. of samples used for the testing)
75+
print "\n Classification Report: \n"
76+
print metrics.classification_report(y_test, y_pred)
77+
78+
# Introducing stop words
79+
stopset = set(stopwords.words('english'))
80+
81+
# Two different classifiers: Count, Tfidf vectorization
82+
clf_count = Pipeline([
83+
('vect', CountVectorizer(
84+
stop_words=stopset,
85+
token_pattern=ur"\b[a-z0-9_\-\.]+[a-z][a-z0-9_\-\.]+\b",
86+
)),
87+
('clf', OneVsRestClassifier(MultinomialNB(alpha=0.01))),
88+
])
89+
90+
clf_tfidf = Pipeline([
91+
('vect', TfidfVectorizer(
92+
stop_words=stopset,
93+
token_pattern=ur"\b[a-z0-9_\-\.]+[a-z][a-z0-9_\-\.]+\b",
94+
)),
95+
('clf', OneVsRestClassifier(MultinomialNB(alpha=0.01))),
96+
])
97+
98+
# List of classifiers
99+
clfs = [clf_count, clf_tfidf]
100+
101+
# Run the evaluation/classification
102+
for clf in clfs:
103+
train_and_evaluate(clf, X_train, X_test, y_train, y_test, my_targetname)

0 commit comments

Comments
 (0)