-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclassifier.py
More file actions
72 lines (57 loc) · 2.66 KB
/
Copy pathclassifier.py
File metadata and controls
72 lines (57 loc) · 2.66 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
import pandas as pd
import re
import numpy as np
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectKBest, chi2
from sqlite3 import Error
from sklearn.ensemble import RandomForestClassifier
import sqlite3
import pickle
#connecting tot he database to load the data. the below database contains the commit messages and their corresponding labels
try:
conn = sqlite3.connect("training_V2.db")
except Error as e:
print(e)
#reading the data from the table that contains the labels
df = pd.read_sql_query('SELECT * FROM filtered', conn)
df.drop(['id'], 1, inplace=True)
# print(df.head())
# this block preprocess the text, removes the stop words and transform the text into vector space using tfidf vectorizer
stemmer = PorterStemmer()
words = stopwords.words("english")
vectorizer = TfidfVectorizer(min_df= 3, stop_words="english", sublinear_tf=True)
df['cleaned'] = df['text'].apply(lambda x: " ".join([stemmer.stem(i) for i in re.sub("[^a-zA-Z]", " ", x).split() if i not in words]).lower())
# this block is to split the dataset into training and testing set
X = df['cleaned']
Y = df['class']
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.20)
pipeline = Pipeline([('vect', vectorizer),
('chi', SelectKBest(chi2, k=1000)),
('clf', RandomForestClassifier())])
# fitting our model and save it in a pickle for later use
model = pipeline.fit(X_train, y_train)
with open('RandomForest.pickle', 'wb') as f:
pickle.dump(model, f)
ytest = np.array(y_test)
# confusion matrix and classification report(precision, recall, F1-score)
print(confusion_matrix(ytest, model.predict(X_test)))
print(classification_report(ytest, model.predict(X_test)))
vectorizer = model.named_steps['vect']
chi = model.named_steps['chi']
clf = model.named_steps['clf']
feature_names = vectorizer.get_feature_names()
feature_names = [feature_names[i] for i in chi.get_support(indices=True)]
feature_names = np.asarray(feature_names)
# in this case, I have 5 different classes:
target_names = ['1', '2', '3', '4', '5']
print("top 10 keywords per class:")
for i, label in enumerate(target_names):
top10 = np.argsort(clf.feature_importances_)[-10:]
print("%s: %s" % (label, " ".join(feature_names[top10])))
# print("accuracy score: " + str(model.score(X_test, y_test)))
# print(model.predict(['FIXED HUDSON-4245] Fixed a bug in the tabular display of a matrix. Will be in 1.323.']))