-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextCategoriesClassification.py
More file actions
61 lines (40 loc) · 1.86 KB
/
textCategoriesClassification.py
File metadata and controls
61 lines (40 loc) · 1.86 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
import sklearn.datasets as skd
#Loading the data
categories = ['talk.politics.misc', 'rec.sport.baseball','comp.graphics', 'sci.electronics']
data_train = skd.load_files('/Users/mac/Documents/Coding/ML/Bays/20news-bydate/20news-bydate-train', categories= categories, encoding= 'ISO-8859-1')
data_test = skd.load_files('/Users/mac/Documents/Coding/ML/Bays/20news-bydate/20news-bydate-test',categories= categories, encoding= 'ISO-8859-1')
from sklearn.feature_extraction.text import CountVectorizer
""" a little example to understand the methods M using
#my text
text = ["I love cats more than anyone in the world.",
"The cat.",
"world"]
#give a unique id for every word and then count how much this id is frequent in each catégori
counter = CountVectorizer()
counter.fit(text)
print("Vocabulary "+str(counter.vocabulary_))
counter.get_feature_names()
print("features names: "+str(counter.get_feature_names()))
counts = counter.transform(text)
print("Counts shape :"+str(counts.shape))
print("Counts:"+str(counts.toarray())) """
counterV = CountVectorizer()
x_train = counterV.fit_transform(data_train)
x_train.shape
from sklearn.feature_extraction.text import TfidTransformer
transformer = TfidTransformer()
x_trainTfid = transformer.fit_transform(x_train)
x_trainTfid.shape
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB().fit(x_trainTfid, x_train)
#Teste
x_teste = counterV.transform(data_test.data)
x_testeTfid = transformer.transform(x_teste)
predicted = clf.predict(x_testeTfid)
from sklearn import metrics
from sklearn.metrics import accuracy_score
print("Accuray:", accuracy_score(data_test.target, predicted))
print(metrics.classification_report(data_test.target, predicted, target_names=data_test.target_names))
vectorizer.fit(counts)
print("Learnin frequency of all features:" +str(vectorizer.idf_)+'\n\n')
freq = vectorizer.transform(counts)